Hello
How can I change the function to get the rows with the sum (x+y+z) = 10?
Thank you very much
Joseph



----- Original Message ----
From: Marc Schwartz <[EMAIL PROTECTED]>
To: joseph <[EMAIL PROTECTED]>
Cc: r-help@r-project.org
Sent: Wednesday, September 3, 2008 3:24:58 PM
Subject: Re: [R] subsetting a data frame

on 09/03/2008 05:06 PM joseph wrote:
> I have a data frame that looks like this:
> V1 V2 V3
> a    b    0:1:12
> d    f    1:2:1
> c    d    1:0:9
> where V3 is in the form x:y:z
> Can someone show me how to subset the rows where the values of x, y and z <= 
> 10: 
> V1 V2 V3
> d    f    1:2:1
> c    d    1:0:9
> Thanks
> Joseph


How about this:



> DF[sapply(strsplit(as.character(DF$V3), ":"),
            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':

> strsplit(as.character(DF$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:

> sapply(strsplit(as.character(DF$V3), ":"),
         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


      
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to