Reputation: 63
Hi everyone I need a help.
I have the data-set similar to this which contain multiple rows and columns.
df<- data.frame(A=seq(0.01,0.05,0.01),
B=c(0.01, -0.24, 0, -0.21, 0),
C=seq(0.03,0.07,0.01),
D=c(0.4,0.5,0,0,2))
I used shift command and created another row E.
df[ , E := shift(A)+A]
Now I want to apply similar function to whole data frame df and create row F, G, H similar to E using similar method at once.
Thank you.
Upvotes: 0
Views: 139
Reputation: 887128
If we are using data.table
, the shift
can take multiple columns at once and output the lag
of those. If we are not selecting any particular sets of columns, specifying shift(.SD)
(.SD
represents the Subset of Data.table) gives the lag
of all the columns in the dataset. By assigning (:=
) it to different column names (LETTERS[5:8]
), we get a new set of lag columns in the original dataset.
library(data.table)
setDT(df)[, LETTERS[5:8] := shift(.SD)+.SD]
df
# A B C D E F G H
#1: 0.01 0.01 0.03 0.4 NA NA NA NA
#2: 0.02 -0.24 0.04 0.5 0.03 -0.23 0.07 0.9
#3: 0.03 0.00 0.05 0.0 0.05 -0.24 0.09 0.5
#4: 0.04 -0.21 0.06 0.0 0.07 -0.21 0.11 0.0
#5: 0.05 0.00 0.07 2.0 0.09 -0.21 0.13 2.0
Or we can loop through lapply
setDT(df)[, LETTERS[5:8] := lapply(.SD, function(x) x+shift(x))]
Upvotes: 3
Reputation: 3427
Here is an alternative for this:
new_cols <- c('E','F','G','H')
old_cols <- colnames(df)
for (i in seq_along(new_cols)){
eval(parse(text = paste0("df[,",new_cols[i],":= shift(",old_cols[i],")+",old_cols[i],"]")))
}
Upvotes: 1