Elegantly assigning multiple columns in data.table with lapply()

后端 未结 3 958
迷失自我
迷失自我 2020-11-28 21:14

I am trying to figure out an elegant way to use := assignment to replace many columns at once in a data.table by applying a shared function. A typi

3条回答
  •  孤独总比滥情好
    2020-11-28 21:57

    Is this what you are looking for?

    dt[ , names(dt)[20:100] :=lapply(.SD, function(x) sqrt(x) ) , .SDcols=20:100]
    

    I have heard tell that using .SD is not so efficient because it makes a copy of the table beforehand, but if your table isn't huge (obviously that's relative depending on your system specs) I doubt it will make much of a difference.

提交回复
热议问题