[R] group calculations with other columns for the ride
Hello, I can get the median for each factor, but I'd like another column to go with each factor. The nm column is a long name for the lvls column. So unique work except for the order can get messed up. Example: x = data.frame(val=1:10,lvls=c('cat2',rep(cat1,4),rep(cat2,4),'cat1'),nm=c('longname2',rep(longname1,4),rep(longname2,4),'longname1')) x val lvlsnm 11 cat2 longname2 22 cat1 longname1 33 cat1 longname1 44 cat1 longname1 55 cat1 longname1 66 cat2 longname2 77 cat2 longname2 88 cat2 longname2 99 cat2 longname2 10 10 cat1 longname1 unique doesn't work in data.frame: mdn = do.call(rbind,lapply(split(x[,1], x[,2]), median)) data.frame(mdn,ln=as.character(unique(x[,3]))) mdnln cat1 4 longname2 cat2 7 longname1 I want: mdnln cat1 4 longname1 cat2 7 longname2 Thank you very much! PS - looking for simple'ish solutions. I know I can do it with loops and merges, but is there an option I am not using here? Ben [[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.
Re: [R] group calculations with other columns for the ride
aggregate(val~lvls+nm,data=x,FUN='median') On Tue, Feb 28, 2012 at 4:43 PM, Ben quant ccqu...@gmail.com wrote: Hello, I can get the median for each factor, but I'd like another column to go with each factor. The nm column is a long name for the lvls column. So unique work except for the order can get messed up. Example: x = data.frame(val=1:10,lvls=c('cat2',rep(cat1,4),rep(cat2,4),'cat1'),nm=c('longname2',rep(longname1,4),rep(longname2,4),'longname1')) x val lvls nm 1 1 cat2 longname2 2 2 cat1 longname1 3 3 cat1 longname1 4 4 cat1 longname1 5 5 cat1 longname1 6 6 cat2 longname2 7 7 cat2 longname2 8 8 cat2 longname2 9 9 cat2 longname2 10 10 cat1 longname1 unique doesn't work in data.frame: mdn = do.call(rbind,lapply(split(x[,1], x[,2]), median)) data.frame(mdn,ln=as.character(unique(x[,3]))) mdn ln cat1 4 longname2 cat2 7 longname1 I want: mdn ln cat1 4 longname1 cat2 7 longname2 Thank you very much! PS - looking for simple'ish solutions. I know I can do it with loops and merges, but is there an option I am not using here? Ben [[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. __ 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.
Re: [R] group calculations with other columns for the ride
Excellent! I wonder why I haven't seen aggregate before. Thanks! ben On Tue, Feb 28, 2012 at 4:51 PM, ilai ke...@math.montana.edu wrote: aggregate(val~lvls+nm,data=x,FUN='median') On Tue, Feb 28, 2012 at 4:43 PM, Ben quant ccqu...@gmail.com wrote: Hello, I can get the median for each factor, but I'd like another column to go with each factor. The nm column is a long name for the lvls column. So unique work except for the order can get messed up. Example: x = data.frame(val=1:10,lvls=c('cat2',rep(cat1,4),rep(cat2,4),'cat1'),nm=c('longname2',rep(longname1,4),rep(longname2,4),'longname1')) x val lvlsnm 11 cat2 longname2 22 cat1 longname1 33 cat1 longname1 44 cat1 longname1 55 cat1 longname1 66 cat2 longname2 77 cat2 longname2 88 cat2 longname2 99 cat2 longname2 10 10 cat1 longname1 unique doesn't work in data.frame: mdn = do.call(rbind,lapply(split(x[,1], x[,2]), median)) data.frame(mdn,ln=as.character(unique(x[,3]))) mdnln cat1 4 longname2 cat2 7 longname1 I want: mdnln cat1 4 longname1 cat2 7 longname2 Thank you very much! PS - looking for simple'ish solutions. I know I can do it with loops and merges, but is there an option I am not using here? Ben [[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. [[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.