How to apply same function to every specified column in a data.table

后端 未结 7 2316
北海茫月
北海茫月 2020-11-22 08:00

I have a data.table with which I\'d like to perform the same operation on certain columns. The names of these columns are given in a character vector. In this particular ex

7条回答
  •  孤独总比滥情好
    2020-11-22 08:24

    library(data.table)
    (dt <- data.table(a = 1:3, b = 1:3, d = 1:3))
    
    Hence:
    
       a b d
    1: 1 1 1
    2: 2 2 2
    3: 3 3 3
    
    Whereas (dt*(-1)) yields:
    
        a  b  d
    1: -1 -1 -1
    2: -2 -2 -2
    3: -3 -3 -3
    

提交回复
热议问题