tags:

views:

99

answers:

2

I want to add a variable (column) to a dataframe, containing in each row the max value of that row across 2nd to 26th column.

For the first row, the code would be:

df$max[1] <- max(df[1,2:26])

I am looking for a way to generalize that for rows 1 to 865. If I give

df$max[1:865] <- max(df[1:865, 2:26])

I get the overall max across all rows for the variable df$max.

Thanks!

Roberto

+5  A: 

You can use apply. For instance:

df[, "max"] <- apply(df[, 2:26], 1, max)

Here's a basic example:

> df <- data.frame(a=1:50, b=rnorm(50), c=rpois(50, 10))
> df$max <- apply(df, 1, max)
> head(df, 2)
  a          b  c max
1 1  1.3527115  9   9
2 2 -0.6469987 20  20
> tail(df, 2)
    a          b  c max
49 49 -1.4796887 10  49
50 50  0.1600679 13  50
Shane
Many thanks Shane!
Roberto
+2  A: 

Vectorized version with pmax:

do.call(pmax, df[2:26])
Marek
+1 Forgot about pmax function.
Shane