I am working with trade dataset with thousands of rows. Every record has a unique key based on a symbol and date. Trade records for a given symbol are irregular, hence using zoo will be natural choice. I need to use lag and merge to create a new dataset. However, I don't know how to setup multi-column index in zoo in order to use lag function. Below is a sample dataset and intended output.
df = data.frame(
dt = as.Date(c("2015-01-01", "2015-01-05", "2015-01-06",
"2015-01-01", "2015-01-02")),
id = c("i1", "i1", "i1", "i2", "i2"),
v1 = c(110, 115, 119, 212, 213),
v2 = c(100, 170, 180, 202, 210),
v3 = c(11, 13, 16, 22, 24)
)
df$id = as.character(df$id)
And the output should be
2015-01-01, i1, 110, 100, 11, 2015-01-05, i1, 115, 170, 13
2015-01-05, i1, 115, 170, 13, 2015-01-06, i1, 119, 180, 16
2015-01-06, i1, 119, 180, 16, NA, NA, NA, NA, NA
2015-01-01, i2, 212, 202, 22, 2015-01-02, i2, 213, 210, 24
2015-01-02, i2, 213, 210, 24, NA, NA, NA, NA, NA
In SO, there are number of posts accomplishing "grouped" lag operations but for a single column only. I am looking for merging complete row, regardless of number of columns.
Update to this question...
Following is one possible way to solve the "grouped" lag operation based on zoo.
doProcessing = function(df){
icolnames = colnames(df)
tt = zoo(df, df$dt)
tt1 = merge(tt, lag(tt, 1))
colnames(tt1) = c(icolnames, paste0("lag_", icolnames))
data.frame(tt1, stringsAsFactors=F)
}
fin_df = do.call(rbind, with(df, by(df, list(id), doProcessing, simplify=F)))
This final output frame has every field as factor. How do I get the output structure right as per input data frame?
Based on @Grothendieck's idea of lapply, a possible solution to the above problem is given below.
doProcessing = function(df){
icolnames = colnames(df)
tt = zoo(df, df$dt)
tt1 = merge(tt, lag(tt, 1))
colnames(tt1) = c(icolnames, paste0("lag_", icolnames))
data.frame(tt1, stringsAsFactors=F)
}
fin_df = do.call(rbind, with(df, by(df, list(id), doProcessing, simplify=F)))
Still need some help, some how resultant data frame has every column as factors. How do I get original structure back?
original data frame structure
> str(df)
'data.frame': 5 obs. of 5 variables:
$ dt: Date, format: "2015-01-05" "2015-01-01" ...
$ id: chr "i1" "i1" "i1" "i2" ...
$ v1: num 115 110 119 212 213
$ v2: num 170 100 180 202 210
$ v3: num 13 11 16 22 24
resultant data frame looks like
> str(fin_df)
'data.frame': 5 obs. of 10 variables:
$ dt : Factor w/ 4 levels "2015-01-01","2015-01-05",..: 1 2 3 1 4
$ id : Factor w/ 2 levels "i1","i2": 1 1 1 2 2
$ v1 : Factor w/ 5 levels "110","115","119",..: 1 2 3 4 5
$ v2 : Factor w/ 5 levels "100","170","180",..: 1 2 3 4 5
$ v3 : Factor w/ 5 levels "11","13","16",..: 1 2 3 4 5
$ lag_dt: Factor w/ 3 levels "2015-01-05","2015-01-06",..: 1 2 NA 3 NA
$ lag_id: Factor w/ 2 levels "i1","i2": 1 1 NA 2 NA
$ lag_v1: Factor w/ 3 levels "115","119","213": 1 2 NA 3 NA
$ lag_v2: Factor w/ 3 levels "170","180","210": 1 2 NA 3 NA
$ lag_v3: Factor w/ 3 levels "13","16","24": 1 2 NA 3 NA