Use do.call
to call pmax
to compare all the columns together for each row value, e.g.:
dat <- data.frame(a=1:5,b=rep(3,5))
# a b
#1 1 3
#2 2 3
#3 3 3
#4 4 3
#5 5 3
do.call(pmax,dat)
#[1] 3 3 3 4 5
When you call pmax
on an entire data.frame directly, it only has one argument passed to the function and nothing to compare it to. So, it just returns the supplied argument as it must be the maximum. It works for non-numeric and numeric arguments, even though it may not make much sense:
pmax(7)
#[1] 7
pmax("a")
#[1] "a"
pmax(data.frame(1,2,3))
# X1 X2 X3
#1 1 2 3
Using do.call(pmax,...)
with a data.frame means you pass each column of the data.frame as a list of arguments to pmax
:
do.call(pmax,dat)
is thus equivalent to:
pmax(dat$a, dat$b)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…