I have huge data.frame
, a sample of which looks as shown below:
df <- read.table(header=TRUE, text="
id BSheetyearlag sd
1001 Mar-1997 0.50
1001 Mar-1997 0.40
1001 Mar-1997 0.30
1001 Mar-1997 0.20
1001 Mar-1997 0.10
1001 Mar-1998 0.20
1001 Mar-1998 0.30", stringsAsFactors=FALSE)
I want to lag the column sd
by 4 days and I run the following codes:
df <- as.data.table(df)
df[, lag_sd := lag(as.zoo(sd), k=4, na.pad=T), by=c("id", "BSheetyearlag")]
It runs well. However, when I want to proceed or to see how my data.frame
looks like (using for instance head
function):
df <- as.data.frame(df)
head(df)
I get the following error:
Error in
order(x, ..., na.last = na.last, decreasing = decreasing)
: argument 1 is not a vector
Does anyone know why it happens?
Here's the sessionInfo()
output:
> sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
other attached packages:
[1] pROC_1.7.1 gdata_2.13.2 TTR_0.22-0 xts_0.9-7
[5] zoo_1.7-11 data.table_1.9.2 nleqslv_2.1.1 plyr_1.8.1
loaded via a namespace (and not attached):
[1] grid_3.0.3 gtools_3.3.1 lattice_0.20-27 Rcpp_0.11.1
[5] reshape2_1.2.2 stringr_0.6.2