如何在r中的data.table中评估(或创建)动态列

时间:2013-03-29 23:10:12

标签: r data.table

我想创建一个新的data.table,或者只是在data.table中添加一些列。指定多个新列很容易,但如果我希望第三列根据我正在创建的列之一计算值,会发生什么。我认为plyr包可以做一些像这样的事情。我们可以在data.table中执行这样的迭代(顺序)列创建吗?

我想做如下

dt <- data.table(shop = 1:10, income = 10:19*70)
dt[ , list(hope = income * 1.05, hopemore = income * 1.20, hopemorerealistic = hopemore - 100)]  

或者

dt[ , `:=`(hope = income*1.05, hopemore = income*1.20, hopemorerealistic = hopemore-100)]

2 个答案:

答案 0 :(得分:8)

您还可以在<-的调用中使用list,例如

DT <- data.table(a=1:5)


DT[, c('b','d') := list(b1 <- a*2, b1*3)]
DT
   a  b  d
1: 1  2  6
2: 2  4 12
3: 3  6 18
4: 4  8 24
5: 5 10 30

DT[, `:=`(hope = hope <- a+1, z = hope-1)]
DT
   a  b  d hope z
1: 1  2  6    2 1
2: 2  4 12    3 2
3: 3  6 18    4 3
4: 4  8 24    5 4
5: 5 10 30    6 5

答案 1 :(得分:4)

可以在j中使用花括号和分号 有多种方法可以解决,这里有两个例子:

# If you simply want to output: 

dt[ ,
  {hope=income*1.05;
   hopemore=income*1.20;
   list(hope=hope, hopemore=hopemore, hopemorerealistic=hopemore-100)}
]


# if you want to save the values

dt[ , c("hope", "hopemore", "hopemorerealistic")  := 
  {hope=income*1.05;
   hopemore=income*1.20;
   list(hope, hopemore, hopemore-100)}
]
dt
#     shop income   hope hopemore hopemorerealistic
#  1:    1    700  735.0      840               740
#  2:    2    770  808.5      924               824
#  3:    3    840  882.0     1008               908
#  4:    4    910  955.5     1092               992
#  5:    5    980 1029.0     1176              1076
#  6:    6   1050 1102.5     1260              1160
#  7:    7   1120 1176.0     1344              1244
#  8:    8   1190 1249.5     1428              1328
#  9:    9   1260 1323.0     1512              1412
# 10:   10   1330 1396.5     1596              1496