Skip to content

subsetting a data frame

2 messages · joseph, Marc Schwartz

#
on 09/03/2008 05:06 PM joseph wrote:
How about this:
function(i) all(as.numeric(i) <= 10)), ]
  V1 V2    V3
2  d  f 1:2:1
3  c  d 1:0:9


Basically, use strsplit() to break apart 'V3':
[[1]]
[1] "0"  "1"  "12"

[[2]]
[1] "1" "2" "1"

[[3]]
[1] "1" "0" "9"


The use sapply() to crawl the list, converting the elements to numerics
and do the value comparison:
function(i) all(as.numeric(i) <= 10))
[1] FALSE  TRUE  TRUE


The above then returns the logical vector to subset the rows of 'DF'.

HTH,

Marc Schwartz