robgriffin247
2011-Oct-26 10:00 UTC
[R] New column of data filled with the larger value from 2 columns
Hi, I'm sure there is a pretty simple answer to this but I have had my head buried in the R book and on help pages for a while now and I've not made any progress. In simple terms: I have 2 columns of data, column A and column B. I want to create a new column (C) and fill it with the largest value from of A or B on each row. So I want C to contain A if B>A and C to contain B if A=>B Like I said I have tried to look for an answer and I'm sure there is one (or many) out there but I am looking in the wrong places or for the wrong terms so I would really appreciate this help! Thanks, Rob. (I promise that once I have mastered R- hopefully in the near future- I will make up for my sins of asking a basic Q by answering many on here!) -- View this message in context: http://r.789695.n4.nabble.com/New-column-of-data-filled-with-the-larger-value-from-2-columns-tp3940020p3940020.html Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2011-Oct-26 11:53 UTC
[R] New column of data filled with the larger value from 2 columns
robgriffin247 <robgriffin247 <at> hotmail.com> writes:> > Hi, > I'm sure there is a pretty simple answer to this but I have had my head > buried in the R book and on help pages for a while now and I've not made any > progress. > > In simple terms: > I have 2 columns of data, column A and column B. I want to create a new > column (C) and fill it with the largest value from of A or B on each row. >sounds like you want data$C <- pmax(data$A,data$B) (or data <- transform(C,pmax(A,B)))
robgriffin247
2011-Oct-26 12:17 UTC
[R] New column of data filled with the larger value from 2 columns
data$C <- pmax(data$A,data$B) worked perfectly thank you very much -- View this message in context: http://r.789695.n4.nabble.com/New-column-of-data-filled-with-the-larger-value-from-2-columns-tp3940020p3940399.html Sent from the R help mailing list archive at Nabble.com.