我有data.table
set.seed(1)
dat <- data.table(Shift = c(c(0,0,0,1,2,1,1)),
Value = rnorm(7),I.Value = rnorm(7))
dat
Shift Value I.Value
0 -0.6264538 0.7383247
0 0.1836433 0.5757814
0 -0.8356286 -0.3053884
1 1.5952808 1.5117812
2 0.3295078 0.3898432
1 -0.8204684 -0.6212406
1 0.4874291 -2.2146999我希望新列是shift(Value,Shift,fill=0)。因此,结果应该是-
Shift Value I.Value new.value new.I.value
0 -0.6264538 0.7383247 -0.6264538 0.7383247
0 0.1836433 0.5757814 0.1836433 0.5757814
0 -0.8356286 -0.3053884 -0.8356286 -0.3053884
1 1.5952808 1.5117812 0 0
2 0.3295078 0.3898432 1.5952808 1.5117812
1 -0.8204684 -0.6212406 0 0
1 0.4874291 -2.2146999 0.3295078 0.3898432到目前为止我拥有的是:
dat[,`:=` (new.value= shift(Value,Shift,fill = 0),
new.I.value=shift(I.Value,Shift,fill = 0))]但是shift函数返回一个列表,因为参数移位不是长度为1,而是整个向量。因此,我面临这样的错误:
Error in `[.data.table`(dat[Shift == 0, `:=`(new = Value)], !Shift == :
(list) object cannot be coerced to type 'double'我试着通过班车,但也没有工作。
如何仅传递列移位的行值,以便返回向量并得到正确的结果?由于我的工作数据集非常大,我更喜欢使用data.table。
编辑:
由上述函数生成的-The输出是-
Shift Value I.Value grp new2 new3
1: 0 -1.22461261 1.7672873 1 -1.2246126 1.7672873
2: 0 -0.47340064 0.7167075 1 -0.4734006 0.7167075
3: 0 -0.62036668 0.9101742 1 -0.6203667 0.9101742
4: 1 0.04211587 0.3841854 2 0.0000000 0.0000000
5: 2 -0.91092165 1.6821761 3 0.0000000 0.0000000
6: 1 0.15802877 -0.6357365 4 0.0000000 0.0000000
7: 1 -0.65458464 -0.4616447 4 0.1580288 -0.6357365第4行应该作为dat$Shift[4]==1移到第5行,第5行应该作为dat$Shift[5]==2移到第7行。如果我们只是写-
dat[,new:=shift(Value,2,fill=0),new.I:=shift(I.Value,2,fill=0)]将所有行作为n=2向下移动2,而不是硬编码的值,n必须是当前正在处理的行的“shift”列中的值。
发布于 2017-07-17 10:59:57
这个能行吗?
请注意,我优先考虑第5行中的值,因为第6行中的值将移到相同的位置。
shift2 <- function(val,shift,fill){
new_val <- rep(fill,length(val))
indices <- (1:length(val))+shift
indices <- indices[indices <= nrow(dat)]
new_val[rev(indices)]= val[length(indices):1]
new_val
}
dat[,`:=` (new.value= shift2(Value,Shift,fill = 0),
new.I.value=shift2(I.Value,Shift,fill = 0))]
# > dat
# Shift Value I.Value new.value new.I.value
# 1: 0 -0.6264538 0.7383247 -0.6264538 0.7383247
# 2: 0 0.1836433 0.5757814 0.1836433 0.5757814
# 3: 0 -0.8356286 -0.3053884 -0.8356286 -0.3053884
# 4: 1 1.5952808 1.5117812 0.0000000 0.0000000
# 5: 2 0.3295078 0.3898432 1.5952808 1.5117812
# 6: 1 -0.8204684 -0.6212406 0.0000000 0.0000000
# 7: 1 0.4874291 -2.2146999 0.3295078 0.3898432如果您想将第6行优先于第5行:
shift3 <- function(val,shift,fill){
new_val <- rep(fill,length(val))
indices <- (1:length(val))+shift
indices <- indices[indices <= nrow(dat)]
new_val[indices]= val[1:length(indices)]
new_val
}
dat[,`:=` (new.value= shift3(Value,Shift,fill = 0),
new.I.value=shift3(I.Value,Shift,fill = 0))]
#
# Shift Value I.Value new.value new.I.value
# 1: 0 -0.6264538 0.7383247 -0.6264538 0.7383247
# 2: 0 0.1836433 0.5757814 0.1836433 0.5757814
# 3: 0 -0.8356286 -0.3053884 -0.8356286 -0.3053884
# 4: 1 1.5952808 1.5117812 0.0000000 0.0000000
# 5: 2 0.3295078 0.3898432 1.5952808 1.5117812
# 6: 1 -0.8204684 -0.6212406 0.0000000 0.0000000
# 7: 1 0.4874291 -2.2146999 -0.8204684 -0.6212406https://stackoverflow.com/questions/45139456
复制相似问题