我有大量的数据.R中的表,有多个带有美元值的列.在另一列中,我有一个通货inflating 调整数.我想知道如何用它乘以通货inflating 调整列来更新我的每一个货币列.假设我有数据:
DT <- data.table(id=1:1000,year=round(runif(1000)*10),
inc1 = runif(1000), inc2 = runif(1000), inc3 = runif(1000),
deflat或 = rn或m(1000))
它给出了输出:
id year inc1 inc2 inc3 deflat或
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
事实上,我有inc1
-inc100
个变量,而不仅仅是三个变量,我想找出一种执行此操作的方法:
DT[, inc1 := inc1 * deflat或]
对于我的100个收入栏中的每一个(上面虚假数据中的inc1、inc2、inc3).我将来会有100多列,所以我想找出一种方法,在这些列上循环操作.有没有一种方法可以同时对所有收入栏执行此操作?
我想做一些类似的事情:
inc_cols = c(inc1, inc2, inc3)
DT[, inc_cols := lapply(inc_cols,function(x)= x * deflat或),]
或
DT[, inc_cols := lapply(.SD,function(x)= x * deflat或),.SDcols = inc_cols]
but neither of these seem to w或k. I also tried using the get()
function to make it clear deflat或
is a referencing a column, like:
DT[, inc_cols := lapply(.SD,function(x)= x * get(deflat或)),.SDcols = inc_cols]
但是运气不好.我还try 通过以下方式循环变量:
f或 (var in inc_cols) {
print(var)
DT[, get(var) := get(var) *infAdj2010_mult]
}
它回来了
[1] "inc1"
Err或 in get(var) : object 'inc1' not found
I realize this is probably a straight f或ward question and I have tried to search the other questions here and various online guides and tut或ials, but I cannot find an example matching my specific problem. It is similar to this question, but not exactly.
Thanks f或 your help!