我有20x1的数据框df,并添加了一个20x3的滞后变量矩阵。还想添加一个20x3的铅变量矩阵。我怎样才能做到这一点?非常感谢你。创建铅变量矩阵
df <- data.frame(Close = c(1221, 1220, 1220, 1217, 1216, 1218 , 1216, 1216, 1217, 1220, 1219, 1218, 1220, 1216, 1217, 1218, 1218, 1207, 1206, 1205))
n <- NROW(df$Close); m <- 3 #how many bars bck to check
LagMatrixClose <- matrix(nrow = n, ncol = m) #pre-allocate
LagMatrixClose <- matrix(unlist(data.table::shift(df$Close, 1L:m)), ncol=m)
dimnames(LagMatrixClose) <- list(rownames(LagMatrixClose, do.NULL = FALSE, prefix = ""),
colnames(LagMatrixClose, do.NULL = FALSE, prefix = "LagC"))
df <- cbind(df, LagMatrixClose)
EDIT/UPDATE:以下代码添加铅变量的这种基质,但它采用的是for
循环,我宁愿避免出于效率目的,因为这是将被施加到一个大的数据框架不仅创建3个主要变量,而且还创建数百个变量。有人可以考虑替代方案吗?
rowShift <- function(x, shiftLen = 1L) {
r <- (1L + shiftLen):(length(x) + shiftLen)
r[r<1] <- NA
return(x[r]) }
n <- NROW(df$Close); m <- 3 #how many bars bck to check
LeadMatrixClose <- matrix(nrow = n, ncol = m) #pre-allocate
for(i in 1:3) { LeadMatrixClose[,i ] <- rowShift(df$Close,+i) }
dimnames(LeadMatrixClose) <- list(rownames(LeadMatrixClose, do.NULL = FALSE, prefix = ""),
colnames(LeadMatrixClose, do.NULL = FALSE, prefix = "LeadC"))
df <- cbind(df, LeadMatrixClose)
这是创建并添加到DF既滞后和铅矩阵最终输出的样子:
Close LagC1 LagC2 LagC3 LeadC1 LeadC2 LeadC3
1 1221 NA NA NA 1220 1220 1217
2 1220 1221 NA NA 1220 1217 1216
3 1220 1220 1221 NA 1217 1216 1218
4 1217 1220 1220 1221 1216 1218 1216
5 1216 1217 1220 1220 1218 1216 1216
6 1218 1216 1217 1220 1216 1216 1217
7 1216 1218 1216 1217 1216 1217 1220
8 1216 1216 1218 1216 1217 1220 1219
9 1217 1216 1216 1218 1220 1219 1218
10 1220 1217 1216 1216 1219 1218 1220
11 1219 1220 1217 1216 1218 1220 1216
12 1218 1219 1220 1217 1220 1216 1217
13 1220 1218 1219 1220 1216 1217 1218
14 1216 1220 1218 1219 1217 1218 1218
15 1217 1216 1220 1218 1218 1218 1207
16 1218 1217 1216 1220 1218 1207 1206
17 1218 1218 1217 1216 1207 1206 1205
18 1207 1218 1218 1217 1206 1205 NA
19 1206 1207 1218 1218 1205 NA NA
20 1205 1206 1207 1218 NA NA NA
谢谢大卫!这正是我所期待的!用一行代码解决了我的问题,而不是使用'for'循环。 – Krug