Consider the following data frame, df.
name age height eyes
1 bob 25 180 green
2 mike 23 178 green
3 mark 24 169 blue
4 steve 26 183 brown
How can we extract the name column and return the selection as a character vector?
- Wrong: This command will return the
namecolumn as a one-column data frame. - Correct
- Correct
- Correct
- Wrong: This command will return the
nameandagecolumns as a \(4 \times 2\) data frame.
Consider the following data frame, df.
name age height eyes
1 bob 25 180 green
2 mike 23 178 green
3 mark 24 169 blue
4 steve 26 183 brown
How can we extract the height column and return the selection as a data frame?
- Wrong: This command will return the
heightcolumn as a numeric vector. - Wrong: This command will return the
heightcolumn as a numeric vector. - Wrong: This command will return the
ageandheightcolumns as a \(4 \times 2\) data frame. - Correct
- Wrong: This command will try to return a row called “height” and fail because there is no such row in
df.
Which of the following statements about data frames is correct?
A data frame is implemented as a list, where every list slot contains a vector-like object, with the added constraint that all of those vectors must have equal length. This is structure allows us to view data frames as a “rectangular,” table-like structure while still being a list underneath. Data frames are not matrices. Matrices are generalizations of atomic vectors that require every element to share the same type, while data frames are special lists that allow each column to have its own type.
You have a data frame, d, with 10 rows. You run d[1:2] <- 1:3 (attempting to replace the first two columns using a length-3 replacement vector), and R raises an error. Why?
R will recycle a shorter replacement vector to fill a longer selection, but only when the replacement length evenly divides the total number of values needed. Replacing 2 columns across 10 rows requires 20 values, and a length-3 vector doesn’t divide 20 evenly (20 / 3 isn’t a whole number). So, R refuses rather than guessing at an ambiguous fill pattern.