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

前端 未结 4 1187
没有蜡笔的小新
没有蜡笔的小新 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:46

    Since you already have the row numbers as a column in your data.table*, you could just do:

    DT[, `:=`(a1 = max(.SD), a2 = min(.SD)), by = a, .SDcols = paste0("V", 1:10)]
    

    or

    setkey(DT, a)
    DT[J(a), `:=`(a1 = max(.SD), a2 = min(.SD)), .SDcols = paste0("V", 1:10)]
    

    The second option uses the silent by-without-by.

    *of course you could also just use row.names or 1:nrow(DT)

提交回复
热议问题