Create lags with a for-loop in R
Posted
by
cptn
on Stack Overflow
See other posts from Stack Overflow
or by cptn
Published on 2014-06-01T07:47:45Z
Indexed on
2014/06/01
15:27 UTC
Read the original article
Hit count: 203
I've got a data.frame
with stock data of several companies (here it's only two). I want 10 additional columns in my stock data.frame
df
with lagged dates (from -5 days to +5 days) for both companies in my event data.frame. I'm using a for loop which is probably not the best solution, but it works partially.
DATE <- c("01.01.2000","02.01.2000","03.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000","01.01.2000","02.01.2000","04.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000")
RET <- c(-2.0,1.1,3,1.4,-0.2, 0.6, 0.1, -0.21, -1.2, 0.9, 0.3, -0.1,0.3,-0.12)
COMP <- c("A","A","A","A","A","A","A","B","B","B","B","B","B","B")
df <- data.frame(DATE, RET, COMP, stringsAsFactors=F)
df
# DATE RET COMP
# 1 01.01.2000 -2.00 A
# 2 02.01.2000 1.10 A
# 3 03.01.2000 3.00 A
# 4 06.01.2000 1.40 A
# 5 07.01.2000 -0.20 A
# 6 09.01.2000 0.60 A
# 7 10.01.2000 0.10 A
# 8 01.01.2000 -0.21 B
# 9 02.01.2000 -1.20 B
# 10 04.01.2000 0.90 B
# 11 06.01.2000 0.30 B
# 12 07.01.2000 -0.10 B
# 13 09.01.2000 0.30 B
# 14 10.01.2000 -0.12 B
this loop works fine
comp <- as.vector(unique(df$COMP))
mylist <- vector('list', length(comp))
# create lags in DATE
for(i in 1:length(comp)) {
print(i)
comp_i <- comp[i]
df_k <- df[df$COMP %in% comp_i, ] # all trading days of one firm
df_k <- transform(df_k,
DATEm1 = c(NA, head(DATE, -1)),
DATEm2 = c(NA, NA, head(DATE, -2)),
DATEm3 = c(NA, NA, NA, head(DATE, -3)),
DATEm4 = c(NA, NA, NA, NA,head(DATE, -4)),
DATEm5 = c(NA, NA, NA, NA, NA, head(DATE, -5)),
DATEp1 = c(DATE[-1], NA))
#DATEp2 = c(DATE[-2], NA, NA),
#DATEp3 = c(DATE[-3], NA, NA, NA),
#DATEp4 = c(DATE[-4], NA, NA, NA, NA),
#DATEp5 = c(DATE[-5], NA, NA, NA, NA, NA))
mylist[[i]] = df_k
}
df1 <- do.call(rbind, mylist)
But if I add the lines with DATEp2, DATEp3, DATEp4, DATEp5
. the code doesn't work. Can anybody tell me what I'm doing wrong here? Here the code with all the lagged dates.
# create lags in DATE
for(i in 1:length(comp)) {
print(i)
comp_i <- comp[i]
df_k <- df[df$COMP %in% comp_i, ] # all trading days of one firm
df_k <- transform(df_k,
DATEm1 = c(NA, head(DATE, -1)),
DATEm2 = c(NA, NA, head(DATE, -2)),
DATEm3 = c(NA, NA, NA, head(DATE, -3)),
DATEm4 = c(NA, NA, NA, NA,head(DATE, -4)),
DATEm5 = c(NA, NA, NA, NA, NA, head(DATE, -5)),
DATEp1 = c(DATE[-1], NA),
DATEp2 = c(DATE[-2], NA, NA),
DATEp3 = c(DATE[-3], NA, NA, NA),
DATEp4 = c(DATE[-4], NA, NA, NA, NA),
DATEp5 = c(DATE[-5], NA, NA, NA, NA, NA))
mylist[[i]] = df_k
}
df1 <- do.call(rbind, mylist)
© Stack Overflow or respective owner