简体   繁体   English

将R中的特定其他列与data.table相乘多列?

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

I have a large data.table in R with several columns with dollar values. 我在R中有一个大的data.table,其中有几列带有美元值。 In a different column I have an inflation adjustment number. 在另一栏中,我有一个通胀调整数字。 I am trying to figure out how to update each of my monetary columns with it multiplied by the inflation adjustment column. 我试图找出如何用它乘以通胀调整列来更新我的每个货币列。 Suppose I have the data: 假设我有数据:

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

which gives output: 给出输出:

             id year      inc1      inc2       inc3    deflator
   1:    1    8 0.4754808 0.6678110 0.41533976 -0.64126988
   2:    2    2 0.6568746 0.7765634 0.70616373  0.39687915
   3:    3    6 0.8192947 0.9236281 0.90002534 -0.69545700
   4:    4    4 0.7781929 0.1624902 0.17565790  0.05263055
   5:    5    7 0.6232520 0.8024975 0.86449836  0.70781887
  ---                                                     
 996:  996    2 0.9676383 0.2238746 0.19822000  0.78564836
 997:  997    9 0.9877410 0.5783748 0.57497438 -1.63365223
 998:  998    8 0.2220570 0.6500632 0.19814932  1.00260174
 999:  999    3 0.4793767 0.2830457 0.54835581  1.04168818
1000: 1000    8 0.2003476 0.6121637 0.02921505  0.34933690

in reality I have inc1 - inc100 , rather than just three variables and I want to figure out a way to perform this action: 在现实中我有inc1 - inc100 ,而不仅仅是三个变量,我想弄清楚执行此操作的方法:

DT[, inc1 := inc1 * deflator]

for each of my 100 income columns (inc1, inc2, inc3 in the fake data above). 对于我的100个收入列中的每一列(上面的假数据中的inc1,inc2,inc3)。 I will have more than 100 columns in the future, so I would like to figure out a way to loop the action over the columns. 我将来会有超过100个列,所以我想找到一种方法来循环操作列。 Is there a way to do this for all the income columns at once? 有没有办法一次性为所有收入栏做这个?

I would like to do something like: 我想做的事情如下:

inc_cols = c(inc1, inc2, inc3)

DT[, inc_cols := lapply(inc_cols,function(x)= x * deflator),]

or 要么

DT[, inc_cols := lapply(.SD,function(x)= x * deflator),.SDcols = inc_cols]

but neither of these seem to work. 但这些似乎都不起作用。 I also tried using the get() function to make it clear deflator is a referencing a column, like: 我也尝试使用get()函数来明确deflator是一个引用列,如:

DT[, inc_cols := lapply(.SD,function(x)= x * get(deflator)),.SDcols = inc_cols]

but had no luck. 但没有运气。 I also tried to loop through the variables with something like: 我还尝试用以下内容循环变量:

for (var in inc_cols) {
  print(var)
  DT[, get(var) := get(var) *infAdj2010_mult] 
}

which returns 返回

[1] "inc1"
 Error in get(var) : object 'inc1' not found 

I realize this is probably a straight forward question and I have tried to search the other questions here and various online guides and tutorials, but I cannot find an example matching my specific problem. 我意识到这可能是一个直截了当的问题,我试图在这里搜索其他问题和各种在线指南和教程,但我找不到匹配我的具体问题的例子。 It is similar to this question , but not exactly. 它类似于这个问题 ,但并不完全如此。

Thanks for your help! 谢谢你的帮助!

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) 或者使用set的可能选项应该非常快,因为避免了[.data.table的开销(由@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)) 

Since you can use dplyr on data.tables, you could also do: 既然你可以在data.tables上使用dplyr,你也可以这样做:

library(dplyr)
DT %>% mutate_each(funs(.*deflator), starts_with("inc"))

Which will multiply each column of DT that starts with "inc" by the "deflator" column. 这将乘以“deflator”列的“inc”开头的每列DT。

This approach is also quite convenient, but likely slower than using set() : 这种方法也很方便,但可能比使用set()慢:

library(data.table); library(magrittr)
set.seed(42)
DT <- data.table(id=1:1000,year=round(runif(1000)*10),
          inc1 = runif(1000), inc2 = runif(1000), inc3 = runif(1000),
          deflator = rnorm(1000))
vars <- names(DT) %>% .[grepl("inc", .)]
DT[, (vars) := .SD * deflator, .SDcols = vars]
DT[]

        id year         inc1        inc2        inc3   deflator
   1:    1    9  0.212563676  0.24806366  0.06860638  0.2505781
   2:    2    9 -0.017438715 -0.12186792 -0.26241497 -0.2779240
   3:    3    3 -1.414016119 -1.20714809 -0.76920337 -1.7247357
   4:    4    8 -1.082336969 -1.78411512 -1.08720698 -2.0067049
   5:    5    6 -0.644638321 -1.07757416 -0.20895576 -1.2918083
  ---                                                          
 996:  996    1 -0.573551720 -1.93996157 -0.50171303 -2.1569621
 997:  997    5 -0.007899417 -0.01561619 -0.05708009 -0.0920275
 998:  998    1 -0.090975121 -0.30475714 -0.27291825 -0.3974001
 999:  999    5 -0.045984079 -0.01563942 -0.07868934 -0.1383273
1000: 1000    0 -0.785962308 -0.63266975 -0.29247974 -0.8257650

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM