[R] Select a group of data from a file

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Fri Jan 18 10:51:37 CET 2008


> I've a file with several data six variables, three quantitative and 
> three qualitative, I would like to select a group of data from the 
> file to analyze then, i.e:
> my file is like that (but with 6 variables):
> 
> Var1   Var2
> 2       1
> 5       1
> 8       1
> 7       2
> 3       2
> 8       2
> 
> I want to use only the data where var2 is "1"
> 2       1
> 5       1
> 8       1
> 
> Exist a way in R to create a new dataframe with a selection of data 
> from other dataframe?

#Your data frame
df1 <- data.frame(Var1 = c(2,5,8,7,3,8), Var2=rep(1:2, each=3))

#The desired frame
df2 <- df1[df1$Var2==1,]

#clearer syntax using the subset function
df3 <- subset(df1, Var2==1)

identical(df2, df3)     #TRUE

Please read section 2.7 of the 'Introduction to R' manual
http://cran.r-project.org/doc/manuals/R-intro.pdf

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}




More information about the R-help mailing list