Re: [R] Selecting a maximum value in same ID
Lee wrote Hi, I am struggling with this issue and need some helps. The data set 'DF' includes persons' IDs and other variables A, B, and C. Each Person has multiple values in A, B, and C. What I am trying to do is 1) selecting a maximum value of B within same ID, and 2) making a new data set (DF.2) that select rows aligning with the maximum value of B. DF and DF.2 are below. I've used functions combining which.max, subset, and loop, but it did not work. If you have ideas, please help. DF IDABC 1 12 36 2 1 15 30 2 2 56 11 2 233 30 2 383 23 2 3587 2 4752 2 482 36 2 577 35 2 575 23 2 673 10 2 676 35 2 775 14 2 721 30 2 814 11 2 846 11 2 875 11 2 830 36 2 921 35 2 975 23 2 DF.2 IDABC 1 12 36 2 233 30 2 383 23 2 482 36 2 577 35 2 676 35 2 721 30 2 830 36 2 921 35 2 Thank you in advance, Lee How about using ddply? library(plyr) txt -ID A B C 1 12 36 2 1 15 30 2 2 56 11 2 2 33 30 2 3 83 23 2 3 58 7 2 4 75 2 2 4 82 36 2 5 77 35 2 5 75 23 2 6 73 10 2 6 76 35 2 7 75 14 2 7 21 30 2 8 14 11 2 8 46 11 2 8 75 11 2 8 30 36 2 9 21 35 2 9 75 23 2 d - read.table(textConnection(txt), header = TRUE) closeAllConnections() ddply(d,~ID,function(x){x[which.max(x$B),]}) # Returns ID A B C 1 1 12 36 2 2 2 33 30 2 3 3 83 23 2 4 4 82 36 2 5 5 77 35 2 6 6 76 35 2 7 7 21 30 2 8 8 30 36 2 9 9 21 35 2 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Selecting-a-maximum-value-in-same-ID-tp4686492p4686500.html Sent from the R help mailing list archive at Nabble.com. __ 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] Selecting a maximum value in same ID
Hi, You could do this either: DF[unlist(with(DF,tapply(B,list(ID),FUN=function(x) x %in% max(x,] #or DF[unlist(with(DF,by(B,ID,FUN=function(x) x%in% max(x,] #or library(plyr) DF[ddply(DF,.(ID),summarize,B %in% max(B))[,2],] A.K. Hi, I am struggling with this issue and need some helps. The data set 'DF' includes persons' IDs and other variables A, B, and C. Each Person has multiple values in A, B, and C. What I am trying to do is 1) selecting a maximum value of B within same ID, and 2) making a new data set (DF.2) that select rows aligning with the maximum value of B. DF and DF.2 are below. I've used functions combining which.max, subset, and loop, but it did not work. If you have ideas, please help. DF ID A B C 1 12 36 2 1 15 30 2 2 56 11 2 2 33 30 2 3 83 23 2 3 58 7 2 4 75 2 2 4 82 36 2 5 77 35 2 5 75 23 2 6 73 10 2 6 76 35 2 7 75 14 2 7 21 30 2 8 14 11 2 8 46 11 2 8 75 11 2 8 30 36 2 9 21 35 2 9 75 23 2 DF.2 ID A B C 1 12 36 2 2 33 30 2 3 83 23 2 4 82 36 2 5 77 35 2 6 76 35 2 7 21 30 2 8 30 36 2 9 21 35 2 Thank you in advance, Lee __ 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.