Information Technology Reference
In-Depth Information
dfrm[[ n ]]
Returns one column —specifically, the n th column of dfrm .
To select one or more columns and package them in a data frame, use the following
sublist expressions:
dfrm[ n ]
Returns a data frame consisting solely of the n th column of dfrm .
dfrm[ c ( n 1 , n 2 , ..., n k )]
Returns a data frame built from the columns in positions n 1 , n 2 , ..., n k of dfrm .
You can use matrix-style subscripting to select one or more columns:
dfrm[, n ]
Returns the n th column (assuming that n contains exactly one value).
dfrm[, c ( n 1 , n 2 , ..., n k )]
Returns a data frame built from the columns in positions n 1 , n 2 , ..., n k .
Note that the matrix-style subscripting can return two different data types (either col-
umn or data frame) depending on whether you select one column or multiple
columns.
Discussion
There are a bewildering number of ways to select columns from a data frame. The
choices can be confusing until you understand the logic behind the alternatives. As you
read this explanation, notice how a slight change in syntax—a comma here, a double-
bracket there — changes the meaning of the expression.
Let's play with the population data for the 16 largest cities in the Chicago metropolitan
area:
> suburbs
city county state pop
1 Chicago Cook IL 2853114
2 Kenosha Kenosha WI 90352
3 Aurora Kane IL 171782
4 Elgin Kane IL 94487
5 Gary Lake(IN) IN 102746
6 Joliet Kendall IL 106221
7 Naperville DuPage IL 147779
8 Arlington Heights Cook IL 76031
9 Bolingbrook Will IL 70834
10 Cicero Cook IL 72616
11 Evanston Cook IL 74239
12 Hammond Lake(IN) IN 83048
13 Palatine Cook IL 67232
14 Schaumburg Cook IL 75386
15 Skokie Cook IL 63348
16 Waukegan Lake(IL) IL 91452
 
Search WWH ::




Custom Search