Information Technology Reference
In-Depth Information
Use simple list notation to select exactly one column, such as the first column:
> suburbs[[1]]
[1] "Chicago" "Kenosha" "Aurora" "Elgin"
[5] "Gary" "Joliet" "Naperville" "Arlington Heights"
[9] "Bolingbrook" "Cicero" "Evanston" "Hammond"
[13] "Palatine" "Schaumburg" "Skokie" "Waukegan"
The first column of suburbs is a vector, so that's what suburbs[[1]] returns: a vector.
If the first column were a factor, we'd get a factor.
The result differs when you use the single-bracket notation, as in suburbs[1]
or suburbs[c(1,3)] . You still get the requested columns, but R wraps them in a data
frame. This example returns the first column wrapped in a data frame:
> suburbs[1]
city
1 Chicago
2 Kenosha
3 Aurora
4 Elgin
5 Gary
6 Joliet
7 Naperville
8 Arlington Heights
9 Bolingbrook
10 Cicero
11 Evanston
12 Hammond
13 Palatine
14 Schaumburg
15 Skokie
16 Waukegan
The next example returns the first and third columns wrapped in a data frame:
> suburbs[c(1,3)]
city pop
1 Chicago 2853114
2 Kenosha 90352
3 Aurora 171782
4 Elgin 94487
5 Gary 102746
6 Joliet 106221
7 Naperville 147779
8 Arlington Heights 76031
9 Bolingbrook 70834
10 Cicero 72616
11 Evanston 74239
12 Hammond 83048
13 Palatine 67232
14 Schaumburg 75386
15 Skokie 63348
16 Waukegan 91452
 
Search WWH ::




Custom Search