prasmas
2012-Nov-23 21:14 UTC
[R] R help..subsetting data frame that meeting multiple criteria
Hi, I am new to R. I am trying to regroup data frame using multiple constrains. for example data frame: data value class percent 15526 36 4.6875 15527 62 85.9375 15527 82 32.4564 15528 36 70.3125 15528 62 9.375 15528 82 74.6875 I need to regroup each class that have greater than or equal to 70 percent into new group. Similarly, I also need to regroup each class that have less than 70 percent into new group. I can do this by using following syntax for each class class36<- data[data$class==36&data$percent>70,] class36a<- data[data$class==36&data$percent<=70,] but I have 100 different classes. In order to do this for all 100 classes, I have write that syntax 100 times. There would be some way to do dynamically to regroup for 100 classes (may be using for loop) but I dont know. Can you please help in this. Output should be like data frame: class36 value class percent 15528 36 70.3125 data frame: class36a value class percent 15526 36 4.6875 data frame: class62 15527 62 85.9375 data frame: class62a 15528 62 9.375 data frame: class82 15528 82 74.6875 data frame: class82a 15527 82 32.4564 Thank you very much your help.. P. -- View this message in context: http://r.789695.n4.nabble.com/R-help-subsetting-data-frame-that-meeting-multiple-criteria-tp4650601.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Nov-24 00:40 UTC
[R] R help..subsetting data frame that meeting multiple criteria
On Nov 23, 2012, at 1:14 PM, prasmas wrote:> Hi, > I am new to R. I am trying to regroup data frame using multiple > constrains. > for example > > data frame: data > value class percent > 15526 36 4.6875 > 15527 62 85.9375 > 15527 82 32.4564 > 15528 36 70.3125 > 15528 62 9.375 > 15528 82 74.6875 > > I need to regroup each class that have greater than or equal to 70 > percent > into new group. Similarly, I also need to regroup each class that > have less > than 70 percent into new group. > > I can do this by using following syntax for each class > class36<- data[data$class==36&data$percent>70,] > class36a<- data[data$class==36&data$percent<=70,] > but I have 100 different classes. In order to do this for all 100 > classes, I > have write that syntax 100 times. There would be some way to do > dynamically > to regroup for 100 classes (may be using for loop) but I dont know. > Can you > please help in this. > Output should be like > data frame: class36 > value class percent > 15528 36 70.3125 > > data frame: class36a > value class percent > 15526 36 4.6875 >> dat.a <- dat[ dat[["class"]] %in% dat[ dat[["percent"]] >70, "class"] , ] > dat.a value class percent 1 15526 36 4.6875 2 15527 62 85.9375 3 15527 82 32.4564 4 15528 36 70.3125 5 15528 62 9.3750 6 15528 82 74.6875 > row.names(dat.a) <- unlist(tapply(dat.a$class, dat.a$class, function(x) paste0(x, letters[1:length(x)]))) > dat.a value class percent 36a 15526 36 4.6875 36b 15527 62 85.9375 62a 15527 82 32.4564 62b 15528 36 70.3125 82a 15528 62 9.3750 82b 15528 82 74.6875 You can split by the NROW of dat.a if you want. -- David. >> data frame: class62 > 15527 62 85.9375 > > data frame: class62a > 15528 62 9.375 > > data frame: class82 > 15528 82 74.6875 > > data frame: class82a > 15527 82 32.4564 > > Thank you very much your help.. > P. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/R-help-subsetting-data-frame-that-meeting-multiple-criteria-tp4650601.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Alameda, CA, USA
Rui Barradas
2012-Nov-24 00:46 UTC
[R] R help..subsetting data frame that meeting multiple criteria
Hello, Like the following? dat <- read.table(text=" value class percent 15526 36 4.6875 15527 62 85.9375 15527 82 32.4564 15528 36 70.3125 15528 62 9.375 15528 82 74.6875 ", header = TRUE) per70 <- dat$percent > 70 split(dat, list(dat$class, per70)) Hope this helps, Rui Barradas Em 23-11-2012 21:14, prasmas escreveu:> Hi, > I am new to R. I am trying to regroup data frame using multiple constrains. > for example > > data frame: data > value class percent > 15526 36 4.6875 > 15527 62 85.9375 > 15527 82 32.4564 > 15528 36 70.3125 > 15528 62 9.375 > 15528 82 74.6875 > > I need to regroup each class that have greater than or equal to 70 percent > into new group. Similarly, I also need to regroup each class that have less > than 70 percent into new group. > > I can do this by using following syntax for each class > class36<- data[data$class==36&data$percent>70,] > class36a<- data[data$class==36&data$percent<=70,] > but I have 100 different classes. In order to do this for all 100 classes, I > have write that syntax 100 times. There would be some way to do dynamically > to regroup for 100 classes (may be using for loop) but I dont know. Can you > please help in this. > Output should be like > data frame: class36 > value class percent > 15528 36 70.3125 > > data frame: class36a > value class percent > 15526 36 4.6875 > > data frame: class62 > 15527 62 85.9375 > > data frame: class62a > 15528 62 9.375 > > data frame: class82 > 15528 82 74.6875 > > data frame: class82a > 15527 82 32.4564 > > Thank you very much your help.. > P. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/R-help-subsetting-data-frame-that-meeting-multiple-criteria-tp4650601.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
arun
2012-Nov-24 02:32 UTC
[R] R help..subsetting data frame that meeting multiple criteria
Hi David, Tried the solution on a slightly different data: dat <- read.table(text=" value class percent 15526 36 4.6875 15527 62 85.9375 15527 82 32.4564 15528 36 70.3125 15528 62 9.375 15528 82 74.6875 15529 72 50.0000 15530 72 50.0000 ", header = TRUE) dat.a <- dat[ dat[["class"]] %in% dat[ dat[["percent"]] >70, "class"] , ] ?row.names(dat.a) <- unlist(tapply(dat.a$class, dat.a$class, function(x) paste0(x, letters[1:length(x)]))) ?dat.a #??? value class percent #36a 15526??? 36? 4.6875 #36b 15527??? 62 85.9375 #62a 15527??? 82 32.4564 #62b 15528??? 36 70.3125 #82a 15528??? 62? 9.3750 #82b 15528??? 82 74.6875 A.K. ----- Original Message ----- From: David Winsemius <dwinsemius at comcast.net> To: prasmas <prasad4rr at gmail.com> Cc: r-help at r-project.org Sent: Friday, November 23, 2012 7:40 PM Subject: Re: [R] R help..subsetting data frame that meeting multiple criteria On Nov 23, 2012, at 1:14 PM, prasmas wrote:> Hi, > I am new to R.? I am trying to regroup data frame using multiple constrains. > for example > > data frame: data >? value??? class??? percent > 15526??? 36??? 4.6875 > 15527??? 62??? 85.9375 > 15527??? 82??? 32.4564 > 15528??? 36??? 70.3125 > 15528??? 62??? 9.375 > 15528??? 82??? 74.6875 > > I need to regroup each class that have greater than or equal to 70 percent > into new group. Similarly, I also need to regroup each class that have less > than 70 percent into new group. > > I can do this by using following syntax for each class > class36<- data[data$class==36&data$percent>70,] > class36a<- data[data$class==36&data$percent<=70,] > but I have 100 different classes. In order to do this for all 100 classes, I > have write that syntax 100 times. There would be some way to do dynamically > to regroup for 100 classes (may be using for loop) but I dont know. Can you > please help in this. > Output should be like > data frame: class36 > value??? class??? percent > 15528??? 36??? 70.3125 > > data frame: class36a > value??? class??? percent > 15526??? 36??? 4.6875 > > dat.a <- dat[ dat[["class"]] %in% dat[ dat[["percent"]] >70, "class"] , ] > dat.a? value class percent 1 15526? ? 36? 4.6875 2 15527? ? 62 85.9375 3 15527? ? 82 32.4564 4 15528? ? 36 70.3125 5 15528? ? 62? 9.3750 6 15528? ? 82 74.6875> row.names(dat.a) <- unlist(tapply(dat.a$class, dat.a$class, function(x) paste0(x, letters[1:length(x)]))) > dat.a? ? value class percent 36a 15526? ? 36? 4.6875 36b 15527? ? 62 85.9375 62a 15527? ? 82 32.4564 62b 15528? ? 36 70.3125 82a 15528? ? 62? 9.3750 82b 15528? ? 82 74.6875 You can split by the NROW of dat.a if you want. --David.> > data frame: class62 > 15527??? 62??? 85.9375 > > data frame: class62a > 15528??? 62??? 9.375 > > data frame: class82 > 15528??? 82??? 74.6875 > > data frame: class82a > 15527??? 82??? 32.4564 > > Thank you very much your help.. > P. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/R-help-subsetting-data-frame-that-meeting-multiple-criteria-tp4650601.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Alameda, CA, USA ______________________________________________ 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.
arun
2012-Nov-24 02:59 UTC
[R] R help..subsetting data frame that meeting multiple criteria
HI, You could also try this: dat <- read.table(text=" value class percent 15526 36 4.6875 15527 62 85.9375 15527 82 32.4564 15528 36 70.3125 15528 62 9.375 15528 82 74.6875 15529 72 50.0000 15530 72 50.0000 ", header = TRUE) mat1<-as.matrix(dat) row.names(mat1)<-paste(dat$class,ave(dat$percent,dat$class,FUN=function(x) x>70),sep="_") ?mat1 #???? value class percent #36_0 15526??? 36? 4.6875 #62_1 15527??? 62 85.9375 #82_0 15527??? 82 32.4564 #36_1 15528??? 36 70.3125 #62_0 15528??? 62? 9.3750 #82_1 15528??? 82 74.6875 #72_0 15529??? 72 50.0000 #72_0 15530??? 72 50.0000 If you? want to change 0 and 1 to "a" and "b". row.names(mat1)<-gsub("1","b",gsub("0","a",row.names(mat1))) ?row.names(mat1) #[1] "36_a" "62_b" "82_a" "36_b" "62_a" "82_b" "72_a" "72_a" Hope it helps. A.K. ----- Original Message ----- From: prasmas <prasad4rr at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, November 23, 2012 4:14 PM Subject: [R] R help..subsetting data frame that meeting multiple criteria Hi, I am new to R.? I am trying to regroup data frame using multiple constrains. for example data frame: data ? value??? class??? percent 15526??? 36??? 4.6875 15527??? 62??? 85.9375 15527??? 82??? 32.4564 15528??? 36??? 70.3125 15528??? 62??? 9.375 15528??? 82??? 74.6875 I need to regroup each class that have greater than or equal to 70 percent into new group. Similarly, I also need to regroup each class that have less than 70 percent into new group. I can do this by using following syntax for each class class36<- data[data$class==36&data$percent>70,] class36a<- data[data$class==36&data$percent<=70,] but I have 100 different classes. In order to do this for all 100 classes, I have write that syntax 100 times. There would be some way to do dynamically to regroup for 100 classes (may be using for loop) but I dont know. Can you please help in this. Output should be like data frame: class36 value??? class??? percent 15528??? 36??? 70.3125 data frame: class36a value??? class??? percent 15526??? 36??? 4.6875 data frame: class62 15527??? 62??? 85.9375 data frame: class62a 15528??? 62??? 9.375 data frame: class82 15528??? 82??? 74.6875 data frame: class82a 15527??? 82??? 32.4564 Thank you very much your help.. P. -- View this message in context: http://r.789695.n4.nabble.com/R-help-subsetting-data-frame-that-meeting-multiple-criteria-tp4650601.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.