Information Technology Reference
In-Depth Information
To avoid this problem, you can include drop=FALSE in the subscripts; this forces R to
return a data frame:
dfrm[,vec,drop=FALSE]
Now there is no ambiguity about the returned data structure. It's a data frame.
When all is said and done, using matrix notation to select columns from data frames
is not the best procedure. I recommend that you instead use the list operators described
previously. They just seem clearer.
1.11 Selecting Data Frame Columns by Name
Problem
You want to select columns from a data frame according to their name.
Solution
To select a single column, use one of these list expressions:
dfrm[[" name "]]
Returns one column , the column called name .
dfrm$ name
Returns the same, but presented in different syntax.
To select one or more columns and package them in a data frame, use these list
expressions:
dfrm[" name "]
Selects one column and packages it inside a data frame object.
dfrm[ c (" name 1 ", " name 2 ", ..., " name k ")]
Selects several columns and packages them in a data frame.
You can use matrix-style subscripting to select one or more columns:
dfrm[, " name "]
Returns the named column.
dfrm[, c (" name 1 ", " name 2 ", ..., " name k ")]
Selects several columns and packages them in a data frame.
Once again, the matrix-style subscripting can return two different data types (column
or data frame) depending on whether you select one column or multiple columns.
Discussion
All columns in a data frame must have names. If you know the name, it's usually more
convenient and readable to select by name, rather than by position.
 
Search WWH ::




Custom Search