使用.I表示法

时间:2017-01-05 19:35:16

标签: r data.table

我遇到了一个data.table行为怪癖,我想在那里执行逐行操作,但除非我先提供一个虚拟变量,否则不能这样做。是否有一步到位的方法来执行以下操作?

# I will calculate global vectorize max/min when going by `.I`

dt <- data.table(x=runif(10), y=runif(10))
dt[, c("max","min"):=list(max(x,y), min(x,y)), by =.I]
dt

# note max/min are the same across rows

        x          y       max        min
1: 0.9311597 0.89425124 0.9907917 0.06315146
2: 0.8007628 0.59832764 0.9907917 0.06315146
3: 0.6626013 0.90871193 0.9907917 0.06315146
4: 0.5857697 0.18239589 0.9907917 0.06315146

# creating a rowid will do the trick 
# however, this requires three steps (create, calc, delete)

dt <- data.table(x=runif(10), y=runif(10))
dt[, rowid:=1:.N]
dt[, c("max","min"):=list(max(x,y), min(x,y)), by=rowid]
dt

# note each row is correctly calculated 

             x           y rowid       max         min
 1: 0.02321296 0.751962427     1 0.7519624 0.023212956
 2: 0.93987266 0.504301816     2 0.9398727 0.504301816
 3: 0.99621226 0.847503323     3 0.9962123 0.847503323
 4: 0.66251070 0.003959591     4 0.6625107 0.003959591

```

1 个答案:

答案 0 :(得分:1)

这可以使用pmaxpmin

来完成
library(data.table)
dt[, c("max","min"):=list(pmax(x,y), pmin(x,y))]