Dear list member, I have the following data example ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2)) I want to add another variable b, such that the max of 'a' by id is returned i.e data ke becomes ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2),b=c(2,2,2,1,2,2)) Any help will be appreciated. Oluwakemi [[alternative HTML version deleted]]
HI, Try this: ?ke$b<-ave(ke$a,ke$patid,FUN=max) ?ke #? patid a b #1???? 1 1 2 #2???? 1 2 2 #3???? 1 2 2 #4???? 2 1 1 #5???? 3 1 2 #6???? 3 2 2 A.K. ----- Original Message ----- From: Kemi Racheal <kemiadeboye2002 at yahoo.com> To: R-help at r-project.org Cc: Sent: Saturday, November 17, 2012 3:56 AM Subject: [R] manipulating longitudinal data in r Dear list member, I have the following data example ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2)) I want to add another variable b, such that the max of 'a' by id is returned i.e data ke becomes ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2),b=c(2,2,2,1,2,2)) Any help will be appreciated. Oluwakemi ??? [[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.
At 08:56 17/11/2012, Kemi Racheal wrote:>Dear list member, > >I have the following data example >ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2)) > >I want to add another variable b, such that the max of 'a' by id is returned >i.e data ke becomes >ke <- data.frame(patid=c(1,1,1,2,3,3),a=c(1,2,2,1,1,2),b=c(2,2,2,1,2,2)) > >Any help will be appreciated.Dear Kemi It is often easier to do some sorts of manipulations on the wide format of the data. I appreciate that you can always do it both ways.>Oluwakemi > > > [[alternative HTML version deleted]]Michael Dewey info at aghmed.fsnet.co.uk http://www.aghmed.fsnet.co.uk/home.html
You can use ave(), like this: ke$maxa <- ave(ke$a, as.factor(ke$patid), FUN=max) greetings, remko -- View this message in context: http://r.789695.n4.nabble.com/manipulating-longitudinal-data-in-r-tp4649855p4650138.html Sent from the R help mailing list archive at Nabble.com.