Add a variable to a data frame containing max value of each row
Using tidyverse
you can try the following:
considering all numeric columns
df %>%
keep(is.numeric) %>%
rowwise() %>%
mutate(maxval = max(across()))
in your specific case
df %>%
rowwise() %>%
mutate(maxval = max(across(2:26)))
Note: For tons of rows, the rowwise()
operation will slow down your analysis.
Vectorized version with pmax
:
df$max <- do.call(pmax, df[2:26])
In case when you need omit NA
values syntax is:
do.call(pmax, c(df[2:26], list(na.rm=TRUE)))
The second argument of do.call
need to be a list of arguments to function. df
is already list so we concatenate it with na.rm=TRUE
argument (converted to list).
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
Here are two additional methods. The first,in base R, is to combine matrix extraction [
with max.col
, which returns a vector indexing the column position of the maximum value in each row.
df$max <- df[2:26][cbind(seq_len(nrow(df)), max.col(df[2:26]))]
cbind
constructs a matrix indexing the position of the maximum value for each row and [
uses this to extract this value.
The second is to use rowMaxs
in the matrixStats
package. This looks like
library(matrixStats)
rowMaxs(as.matrix(df[2:26])))
Let's do some benchmarking.
# data.frame with 1000 observations and 26 variables
set.seed(1234)
df <- data.frame(id=paste0(letters[-1], 1:40), matrix(rnorm(25000L, 5L, 10L), 1000L))
Also add the rowMaxs
function from the matrixStats
package to the mix.
library(matrixStats)
library(microbenchmark)
microbenchmark(apply=apply(df[, 2:26], 1, max),
pmax=do.call(pmax, df[2:26]),
max.colSub=df[2:26][cbind(seq_len(nrow(df)), max.col(df[2:26]))],
rowMaxs=rowMaxs(as.matrix(df[2:26])))
Unit: microseconds
expr min lq mean median uq max neval cld
apply 1610.540 1786.5905 2193.5334 1863.5680 1990.4380 6915.999 100 c
pmax 354.382 364.6455 380.1720 373.3405 385.4580 567.923 100 a
max.colSub 604.416 651.7430 822.6015 664.7155 681.2510 3086.512 100 b
rowMaxs 243.762 264.0040 320.2350 277.9750 290.5190 2328.712 100 a
So, rowMaxs
is the clear winner followed by pmax
and then by max.col
, with matrix extraction, and apply
at the tail end of the pack.
With a data.frame with 10000 rows and 26 columns, we get a similar story:
set.seed(1234)
df <- data.frame(id=paste0(letters[-1], 1:400), matrix(rnorm(250000L, 5L, 10L), 10000L))
The above code returns
Unit: milliseconds
expr min lq mean median uq max neval cld
apply 15.193361 18.299830 21.737516 20.337880 21.774793 99.44836 100 c
pmax 3.060853 3.101481 3.156630 3.137545 3.191430 3.54182 100 a
max.colSub 3.338828 3.642603 7.051700 3.992708 6.336531 84.43119 100 b
rowMaxs 1.244184 1.322302 2.675281 1.508474 1.638053 79.28054 100 a