我需要计算3列中每一行的最大值。
一张桌子可以是:
x = c(1,2,3,4,5 )
y = c(2,3,3,1,1 )
z = c(4,3,2,1,1 )
df<-data.frame(x,y,z)我要得到:
x y z max
1 1 2 4 4
2 2 3 3 3
3 3 3 2 3
4 4 1 1 4
5 5 1 1 5我试过:
df$max<-max(x, y,z)但我明白:
x y z max
1 1 2 4 5
2 2 3 3 5
3 3 3 2 5
4 4 1 1 5
5 5 1 1 5那么,我如何才能正确地做到这一点呢?
发布于 2014-09-19 14:20:07
使用data.table :)
library(data.table)
x = c(1,2,3,4,5 )
y = c(2,3,3,1,1 )
z = c(4,3,2,1,1 )
dt<-data.table(x,y,z)
dt[, max:=pmax(x,y,z)]
dthttps://stackoverflow.com/questions/25935134
复制相似问题