r - how to retrieve all the records from dataframe with a particular column having two values -
i have data frame called "specdata"
this dataframe specdata consists:
head(specdata) date sulfate nitrate id 1: 2003-01-01 na na 1 2: 2003-01-02 na na 1 3: 2003-01-03 na na 1 4: 2003-01-04 na na 1 5: 2003-01-05 na na 1 6: 2003-01-06 na na 1 tail(specdata) date sulfate nitrate id 1: 2004-12-26 na na 332 2: 2004-12-27 na na 332 3: 2004-12-28 na na 332 4: 2004-12-29 na na 332 5: 2004-12-30 na na 332 6: 2004-12-31 na na 332
the data frame contains id column 1 332 , sulfate has values other na's.
when tried retrieve values of sulfate column id column==1, worked used below code that:
subset(specdata$sulfate,specdata$id==1)
what need should able retrieve values of sulfate column id = 1:20, didn't worked.
i used below code that:
subset(specdata$sulfate,specdata$id==1) warning message: in specdata$id == 1:20 : longer object length not multiple of shorter object length
can 1 me on this?
if subsetting single vector,
specdata$sulfate[specdata$id %in% 1:20]
should work fine. subset
more useful subsetting entire data.frame like
subset(specdata, id %in% 1:20)
and allows skip explicit indexing ("$" or "[,]") though not work when try use variables column names.
Comments
Post a Comment