row-by-row operations and updates in data.table

前端 未结 4 1189
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 10:39

I ended up with a big data.table and I have to do operations per row. (yes... I know that this is clearly not what data.table are for)

R) set.seed(1)
R) DT=d         


        
4条回答
  •  遥遥无期
    2020-12-09 11:42

    Am I missing something, doesn't this give the min across row

    set.seed(1)
    DT=data.table(matrix(rnorm(100),nrow=10))
    DT[,c('a','b'):=list(1:10,2:11)]
    DT
    cols<-c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10")
    

    Method 1

    DT[,Min_Vi:=do.call(pmin, c(.SD, na.rm=TRUE)), .SDcols=cols]
    

    Method 2

    transform(DT,Min_Vi=pmin(get(cols)))
    

提交回复
热议问题