You are on page 1of 1

Chapter 12

Selecting Both Variables and Observations

While the sections above have focused on selecting variables and observations
separately, you can combine methods in most cases. For example, analyzing
variables gender through q4 for just the females could be done with any of these
approaches.
Here we select the females by using index numbers 1:4 and then the q
variables by telling it that they are column indexes 2:6.
summary( mydata [ 1:4, 2:6 ] )
Here we do the same selection, but we select the females with a logical
condition on rows and then select the q variables by supplying a character
vector of their names. The only reason we need to attach the data on this one is
that it refers to gender rather than mydata$gender.
attach (mydata)
summary( mydata [
which(gender=="f"),
c("gender", "q1", "q2", "q3", "q4")
])
detach(mydata)
Here we use the subset function, with both its subset and select
arguments. It is often the easiest to use.
summary (
subset(mydata, subset=gender=="f",
select=gender:q4)
)

R.A. Muenchen, R for SAS and SPSS Users, DOI: 10.1007/978-0-387-09418-2_12, 141
Ó Springer ScienceþBusiness Media, LLC 2009

You might also like