Graphics Reference
In-Depth Information
Solution
Use the subset() function. It can be used to pull out rows that satisfy a set of conditions and to
select particular columns.
We'll use the climate data set for the examples here:
library(gcookbook) # For the data set
climate
Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y
Berkeley 1800
NA
NA
-0.435 0.505
Berkeley 1801
NA
NA
-0.453 0.493
Berkeley 1802
NA
NA
-0.460 0.486
...
CRUTEM3 2009
0.7343
NA
NA
NA
CRUTEM3 2010
0.8023
NA
NA
NA
CRUTEM3 2011
0.6193
NA
NA
NA
The following will pull out only rows where Source is "Berkeley" and only the columns named
Year and Anomaly10y :
subset(climate, Source == "Berkeley" , select = c(Year, Anomaly10y))
Year Anomaly10y
1800
-0.435
1801
-0.453
1802
-0.460
...
2002
0.856
2003
0.869
2004
0.884
Discussion
It is possible to use multiple selection criteria, by using the | (OR) and & (AND) operators. For
example, this will pull out only those rows where source is "Berkeley" , between the years
1900 and 2000:
subset(climate, Source == "Berkeley" & Year >= 1900 & Year <= 2000 ,
select = c(Year, Anomaly10y))
Year Anomaly10y
1900
-0.171
1901
-0.162
1902
-0.177
Search WWH ::




Custom Search