Skip to content
Prev 337074 / 398528 Next

find max value in different columns

Hi,
Try:
dat <- read.table(text="ID??? ONE? TWO? THREE
1??? 2????? 5??????? 7
2??? 6????? NA????? NA
3??? 5????? 7??????? NA
4??? 1????? NA????? NA",sep="",header=TRUE)
dat1 <- dat

#Either:
dat$MAX <- apply(dat[,-1],1,max,na.rm=TRUE)


#or
?dat1$MAX <- do.call('pmax',c(dat1[,2:4],list(na.rm=TRUE)))
?identical(dat,dat1)
#[1] TRUE
A.K.
On Thursday, February 27, 2014 2:08 AM, Mat <matthias.weber at fnt.de> wrote:
Hello together,

i have a data.frame like this one:

ID? ? ONE?  TWO?  THREE
1? ?  2? ? ?  5? ? ? ? 7
2? ?  6? ? ?  NA? ? ? NA
3? ?  5? ? ?  7? ? ? ? NA
4? ?  1? ? ?  NA? ? ? NA

Now i want a new column with the max-Value of "ONE", "TWO" and THREE. The
result look like this one:

ID? ? ONE?  TWO?  THREE? ? MAX
1? ?  2? ? ?  5? ? ? ? 7? ? ? ? ?  7
2? ?  6? ? ?  NA? ? ? NA? ? ? ?  6
3? ?  5? ? ?  7? ? ? ? NA? ? ? ?  7
4? ?  1? ? ?  NA? ? ? NA? ? ? ?  1

how can i do this?

thank you. Mat



--
View this message in context: http://r.789695.n4.nabble.com/find-max-value-in-different-columns-tp4685905.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.