Multiply many columns by a specific other column in R with data.table?

前端 未结 3 614
一整个雨季
一整个雨季 2020-12-05 04:21

I have a large data.table in R with several columns with dollar values. In a different column I have an inflation adjustment number. I am trying to figure out how to update

3条回答
  •  感动是毒
    2020-12-05 04:58

    You could try

    DT[, (inc_cols) := lapply(.SD, function(x) 
            x * DT[['deflator']] ), .SDcols = inc_cols]
    head(DT1,2)
    #   id year         inc1         inc2       inc3   deflator
    #1:  1    3  0.614838304  0.009796974  0.3236051  0.7735552
    #2:  2    2 -0.001583579 -0.082289606 -0.1365115 -0.6644330
    

    Or if you need a loop

    for(inc in inc_cols){
      nm1 <- as.symbol(inc)
      DT[,(inc):= eval(nm1)*deflator]
    }
    
     head(DT,2)
     #  id year         inc1         inc2       inc3   deflator
     #1:  1    3  0.614838304  0.009796974  0.3236051  0.7735552
     #2:  2    2 -0.001583579 -0.082289606 -0.1365115 -0.6644330
    

    Or a possible option using set which should be very fast as the overhead of [.data.table is avoided (suggested by @Arun)

    indx <- grep('inc', colnames(DT))
    
    for(j in indx){
     set(DT, i=NULL, j=j, value=DT[[j]]*DT[['deflator']])
    }
    head(DT,2)
    #  id year         inc1         inc2       inc3   deflator
    #1:  1    3  0.614838304  0.009796974  0.3236051  0.7735552
    #2:  2    2 -0.001583579 -0.082289606 -0.1365115 -0.6644330
    

    where

    inc_cols <-  grep('^inc', colnames(DT), value=TRUE)
    

    data

    set.seed(24)
    DT <- data.table(id=1:1000,year=round(runif(1000)*10), 
          inc1 = runif(1000), inc2 = runif(1000), inc3 = runif(1000),    
          deflator = rnorm(1000)) 
    

提交回复
热议问题