I've come across a data.table
behavioral quirk where I wish to perform rowwise operations but cannot do so unless I first provide a dummy variable. Is there a one-step way to do the following?
# 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
```