0

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 
Arun
  • 116,683
  • 26
  • 284
  • 387
Jack
  • 167
  • 2
  • 12
  • Dear Arun, I have added it as edited – Jack Apr 13 '14 at 22:41
  • df is a data.table, not a data.frame. Maybe you have to do `head(as.data.table(df))` ? Experiment with that and look for data.table-specific issues. Also, you should tag this [tag:data.table] – smci Apr 13 '14 at 22:43
  • But prior using head function I am changing df, from data table to data frame. Thanks for comment I will edit it now. – Jack Apr 13 '14 at 22:45
  • Need reproducible example... use `dput()` to export a small snippet of the df, or create a dummy example for us with `runif/rnorm()`. – smci Apr 14 '14 at 03:17
  • This isn't reproducible with data.table >= v1.14.2 – Waldi Jul 02 '22 at 11:03

1 Answers1

0

df is a data.table, not a data.frame.

Workaround: you don't strictly need head(), you can always fall back to manually taking the first 10 (or N) rows manually:

df[1:10,]

I don't know if data.table has issues with head(), but if it does, look for a data.table::print.data.table method or similar. These are the object-specific methods that get dispatched when you call plain print() on an object.

Note that things will differ, like the formatting, whether it respects/ignores options('digits'), options('scipen') etc. This is also true of head() on a dplyr tbl_df.

smci
  • 32,567
  • 20
  • 113
  • 146