Skip to content

select cases from a data.frame or matrix

4 messages · Till Baumgaertel, Peter Dalgaard, Peter Holzer +1 more

#
hi all,

is there a better solution to select rows from a data.frame than by iterating 
through the whole set and evaluating every case one by one? 

is there maybe something like:
d <- data.frame(...)
maleOver40 <- select.data.frame( d, "( sex=m or sex=M) and age > 40)")

of course, i could use direct database-requests, but this would require my data 
to be stored in a database...

thank you,
till

--
"I have seen things you people wouldn't believe.
Attack-ships  on fire off the shoulder of Orion.
I watched seabeams glitter in the dark near the 
Tannhauser gate.
All  those  moments  will  be lost in time like
tears in rain. Time to die."              (Roy)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Till Baumgaertel" <till.baumgaertel at epost.de> writes:
subset(d, sex %in% c("m","M") & age > 40)
#
Till Baumgaertel writes:
 > hi all,
 > 
 > is there a better solution to select rows from a data.frame than by iterating 
 > through the whole set and evaluating every case one by one? 
 > 
 > is there maybe something like:
 > d <- data.frame(...)
 > maleOver40 <- select.data.frame( d, "( sex=m or sex=M) and age > 40)")
 > 
 > of course, i could use direct database-requests, but this would require my data 
 > to be stored in a database...
 > 

maleOver40 <- d[(d$sex == "m" | d$sex == "M") & d$age > 40,]

 > thank you,
 > till
 > 
 > --
 > "I have seen things you people wouldn't believe.
 > Attack-ships  on fire off the shoulder of Orion.
 > I watched seabeams glitter in the dark near the 
 > Tannhauser gate.
 > All  those  moments  will  be lost in time like
 > tears in rain. Time to die."              (Roy)
 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
 > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
 > Send "info", "help", or "[un]subscribe"
 > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Would subset() do what you want?  for example,

maleOver40 <- subset(select.data.frame,  subset=(sex=="m" | sex== "M") & age > 40)

Rashid Nassar
On Wed, 20 Jun 2001, Till Baumgaertel wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._