Skip to content

by cannot cope with pdata.frame converted to data.table #1128

Closed

Description

I found by= returns an error when performed on a former pdata.frame (from plm package). Here is an example:

library(data.table)
set.seed(45L)
DT1 <- data.table(V1=c(1L,2L),
                 V2=LETTERS[1:3],
                 V3=round(rnorm(4),4),
                 V4=1:12)


DT1
   V1 V2      V3 V4
 1:  1  A  0.3408  1
 2:  2  B -0.7033  2
 3:  1  C -0.3795  3
 4:  2  A -0.7460  4
 5:  1  B  0.3408  5
 6:  2  C -0.7033  6
 7:  1  A -0.3795  7
 8:  2  B -0.7460  8
 9:  1  C  0.3408  9
10:  2  A -0.7033 10
11:  1  B -0.3795 11
12:  2  C -0.7460 12

DT1[ , sum(V3), by=c("V2", "V1")]
   V2 V1      V1
1:  A  1 -0.0387
2:  B  2 -1.4493
3:  C  1 -0.0387
4:  A  2 -1.4493
5:  B  1 -0.0387
6:  C  2 -1.449

library(plm)
pDT1 <- pdata.frame(DT1, index=c("V2", "V4"), drop.index=FALSE, row.names=TRUE) # convert to a pdata.frame


DT2 <- as.data.table(pDT1) # convert back to a data.table
is.data.table(DT2)
TRUE

DT2
V1 V2      V3 V4
1   1  A  0.3408  1
2   2  A -0.7460  4
3   1  A -0.3795  7
4   2  A -0.7033 10
5   2  B -0.7033  2
6   1  B  0.3408  5
7   2  B -0.7460  8
8   1  B -0.3795 11
9   1  C -0.3795  3
10  2  C -0.7033  6
11  1  C  0.3408  9
12  2  C -0.7460 12

DT2[ , sum(V3), by=c("V2", "V1")]
Error in `[.pdata.frame`(DT2, , sum(V3), by = c("V2", "V1")) : 
  unused argument (by = c("V2", "V1"))


# Delete attributes introduced by converting to pdata.frame
DT2 <- as.data.table(lapply(DT2, function(x){ attr(x, "index") <- NULL; x}))
DT2 <- as.data.table(lapply(DT2, function(x){ attr(x, "class") <- NULL; x}))

DT2[ , sum(V3), by=c("V2", "V1")] # works now
   V2 V1      V1
1:  1  1 -0.0387
2:  1  2 -1.4493
3:  2  2 -1.4493
4:  2  1 -0.0387
5:  3  1 -0.0387
6:  3  2 -1.4493

plm_1.4-0
data.table_1.9.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions