An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080906/dce8c928/attachment.pl>
subsetting a data frame
4 messages · joseph, Jorge Ivan Velez, stephen sefick
#something like this? V1 <- c(1:10) V2 <- c(0,5,7,8,1,6,5,13,7,0) V3 <- c(9,5,6,8,1,7,5,33,88,0) z <- cbind(V1,V2,V3) row.sums <- rowSums(z) d <- cbind(z, row.sums) subset(d, row.sums==10)
On Sat, Sep 6, 2008 at 2:25 PM, joseph <jdsandjd at yahoo.com> wrote:
Hi Jorge
I got the rows where V3 looks like this 10:10:10; Ithe sum here is 30 and not 10.
I want the rows where the sum is 10 for exaple 5:5:0 and 2:2:6
thanks
Joseph
----- Original Message ----
From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
To: joseph <jdsandjd at yahoo.com>
Sent: Saturday, September 6, 2008 10:43:09 AM
Subject: Re: [R] subsetting a data frame
Dear Joseph,
Try
DF[sapply(strsplit(as.character(DF$V3), ":"),
function(i) all(as.numeric(i) == 10)), ]
HTH,
Jorge
On Sat, Sep 6, 2008 at 1:24 PM, joseph <jdsandjd at yahoo.com> wrote:
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 <marc_schwartz at comcast.net>
To: joseph <jdsandjd at yahoo.com>
Cc: r-help at 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 at 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. [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Stephen Sefick Research Scientist Southeastern Natural Sciences Academy Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080906/53d5990a/attachment.pl>
Sorry I didn't read the problem carefully. On Sat, Sep 6, 2008 at 2:53 PM, Jorge Ivan Velez
<jorgeivanvelez at gmail.com> wrote:
Hi Joseph,
Try this:
# Data set
DF=read.table(textConnection("V1 V2 V3
a b 0:1:12
d f 1:2:1
c d 1:0:9
b e 2:2:6
f c 5:5:0"),header=TRUE)
closeAllConnections()
target=10
DF[sapply(strsplit(as.character(DF$V3), ":"), function(x)
sum(as.numeric(x))== target), ]
HTH,
Jorge
On Sat, Sep 6, 2008 at 2:25 PM, joseph <jdsandjd at yahoo.com> wrote:
Hi Jorge
I got the rows where V3 looks like this 10:10:10; Ithe sum here is 30
and not 10.
I want the rows where the sum is 10 for exaple 5:5:0 and 2:2:6
thanks
Joseph
----- Original Message ----
From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
To: joseph <jdsandjd at yahoo.com>
Sent: Saturday, September 6, 2008 10:43:09 AM
Subject: Re: [R] subsetting a data frame
Dear Joseph,
Try
DF[sapply(strsplit(as.character(DF$V3), ":"),
function(i) all(as.numeric(i) == 10)), ]
HTH,
Jorge
On Sat, Sep 6, 2008 at 1:24 PM, joseph <jdsandjd at yahoo.com> wrote:
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 <marc_schwartz at comcast.net> To: joseph <jdsandjd at yahoo.com> Cc: r-help at 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 at 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.
Stephen Sefick Research Scientist Southeastern Natural Sciences Academy Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis