Graphics Reference
In-Depth Information
...
1998 0.680
1999 0.734
2000 0.748
You can also get a subset of data by indexing into the data frame with square brackets, although
this approach is somewhat less elegant. The following code has the same effect as the code we
just saw. The part before the comma picks out the rows, and the part after the comma picks out
the columns:
climate[climate$Source == "Berkeley" & climate$Year >= 1900 & climate$Year <= 2000 ,
c( "Year" , "Anomaly10y" )]
If you grab just a single column this way, it will be returned as a vector instead of a data frame.
To prevent this, use drop=FALSE , as in:
climate[climate$Source == "Berkeley" & climate$Year >= 1900 & climate$Year <= 2000 ,
c( "Year" , "Anomaly10y" ), drop = FALSE
FALSE ]
Finally, it's also possible to pick out rows and columns by their numeric position. This gets the
second and fifth columns of the first 100 rows:
climate[ 1 : 100 , c( 2 , 5 )]
I generally recommend indexing using names rather than numbers when possible. It makes the
code easier to understand when you're collaborating with others or when you come back to it
months or years after writing it, and it makes the code less likely to break when there are changes
to the data, such as when columns are added or removed.
Changing the Order of Factor Levels
Problem
You want to change the order of levels in a factor.
Solution
The level order can be specified explicitly by passing the factor to factor() and specifying
levels . In this example, we'll create a factor that initially has the wrong ordering:
# By default, levels are ordered alphabetically
sizes <- factor(c( "small" , "large" , "large" , "small" , "medium" ))
sizes
small large large small medium
Search WWH ::




Custom Search