plm::pdata.frame loses variable label attributes created by labelled::var_label?

138 Views Asked by At

I have a panel data to which I have applied plm::pdata.frame to be able to use the lag operator. However, once I have converted by dataframe to pdata.frame, the variable labels that I created using labelled::var_label do not show up in modelsummary::msummary(), rather the variable names show up. Is there anything I can do to make sure that variable labels, not names show up in msummary()?

1

There are 1 best solutions below

3
On

Those "labels" are actually attributes that apparently get lost during conversion. You can try to reassign them to your pdata.frame. Example:

dat <- data.frame(matrix(1:12, 3, 4))  ## toy df
dat[] <- Map(`attr<-`, dat, 'foo', 1:4)  ## assign some labels named 'foo'

str(dat)  ## check structure
# 'data.frame': 3 obs. of  4 variables:
# $ X1: int  1 2 3
# ..- attr(*, "foo")= int 1
# $ X2: int  4 5 6
# ..- attr(*, "foo")= int 2
# $ X3: int  7 8 9
# ..- attr(*, "foo")= int 3


pdat <- plm::pdata.frame(dat)  ## convert to pdata.frame
str(pdat)  ## check for attributes
# Classes ‘pdata.frame’ and 'data.frame':   3 obs. of  3 variables:
# $ X1: Factor w/ 3 levels "1","2","3": 1 2 3
# ..- attr(*, "names")= chr [1:3] "1-4" "2-5" "3-6"
# ..- attr(*, "index")=Classes ‘pindex’ and 'data.frame':   3 obs. of  2 variables:
# .. ..$ X1: Factor w/ 3 levels "1","2","3": 1 2 3
# .. ..$ X2: Factor w/ 3 levels "4","5","6": 1 2 3
# $ X2: Factor w/ 3 levels "4","5","6": 1 2 3
# ..- attr(*, "names")= chr [1:3] "1-4" "2-5" "3-6"
# ..- attr(*, "index")=Classes ‘pindex’ and 'data.frame':   3 obs. of  2 variables:
# .. ..$ X1: Factor w/ 3 levels "1","2","3": 1 2 3
# .. ..$ X2: Factor w/ 3 levels "4","5","6": 1 2 3
# ...

Now we can see, that there are new attributes, but the old are lost. However we can reassign them easily using `attributes<-`():

pdat[] <- Map(`attributes<-`, dat, lapply(dat, attributes))
str(pdat)
# Classes ‘pdata.frame’ and 'data.frame':   3 obs. of  3 variables:
#   $ X1: 'pseries' Named int  1 2 3
# ..- attr(*, "foo")= int 1
# ..- attr(*, "names")= chr [1:3] "1-4" "2-5" "3-6"
# ..- attr(*, "index")=Classes ‘pindex’ and 'data.frame':   3 obs. of  2 variables:
# .. ..$ X1: Factor w/ 3 levels "1","2","3": 1 2 3
# .. ..$ X2: Factor w/ 3 levels "4","5","6": 1 2 3
# $ X2: 'pseries' Named int  4 5 6
# ..- attr(*, "foo")= int 2
# ..- attr(*, "names")= chr [1:3] "1-4" "2-5" "3-6"
# ..- attr(*, "index")=Classes ‘pindex’ and 'data.frame':   3 obs. of  2 variables:
# .. ..$ X1: Factor w/ 3 levels "1","2","3": 1 2 3
# .. ..$ X2: Factor w/ 3 levels "4","5","6": 1 2 3
# ...

`attributes<-`() will assign all attributes of dat to pdat. To only select specific labels, use `attr<-`() with the label name.

Notice that your attributes probably called "labeled" not "foo", I don't use labelled. See if this works and your labels show up in the summary.