从 dataset/variable 中删除自定义(第二个)class

Removing a custom (second) class from a dataset/variable

我一直在使用 hmisc 包中的 class,名为 haven_labelled(有时只是 labelled)。它的目的是从 Stata .dta 数据集中导入列标签。尝试在数据帧上使用 plm 时出现错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘c("pseries", "haven_labelled")’ to a data.frame

类如下:

> class(actualdataset)
[1] "pdata.frame" "data.frame"
> class(actualdataset$examplevar)
[1] "pseries"        "haven_labelled"

因此,我想从此数据库中删除 haven_labelled class。遗憾的是,我无法重现错误。我认为这与我的 actualdataset 中的 var 有关,它是一个双 class,其中包括 haven_labelled。请参阅以下示例数据集。

library(data.table)
library(plm)
library(Hmisc)
set.seed(1)
DT <- data.table(panelID = sample(50,50),                                                    # Creates a panel ID
                      Country = c(rep("A",30),rep("B",50), rep("C",20)),       
                      some_NA = sample(0:5, 6),                                             
                      some_NA_factor = sample(0:5, 6),         
                      Group = c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)),
                      Time = rep(seq(as.Date("2010-01-03"), length=20, by="1 month") - 1,5),
                      norm = round(runif(100)/10,2),
                      Income = sample(100,100),
                      Happiness = sample(10,10),
                      Sex = round(rnorm(10,0.75,0.3),2),
                      Age = round(rnorm(10,0.75,0.3),2),
                      Educ = round(rnorm(10,0.75,0.3),2))           
DT [, uniqueID := .I]                                                                        # Creates a unique ID     
DT[DT == 0] <- NA                                                                            # 
DT$some_NA_factor <- factor(DT$some_NA_factor)
labels <- data.table::fread("Varcode Variables
                         panelID a
                         Country b
                         Group c
                         Time d
                         norm e
                         Income f
                         Happiness g
                         Sex h
                         Age i
                         Educ j
                         uniqueID k                         
                         ", header = TRUE)
for (i in seq_len(ncol(DT))) { 
    label(DT[[i]]) <-  labels$Variables[match(names(DT)[i], labels$Varcode)] 
 }
DTp <- plm::pdata.frame(DT, index= c("panelID", "Time"))
result <- plm(Happiness ~ Income, data=DTp, model="within")

> class(DTp)
[1] "pdata.frame" "data.frame"
> class(DTp$Income)
[1] "pseries"  "labelled" "integer" 

有什么建议吗?

编辑:我在考虑以下事情:

for for (i in seq_len(ncol(DT)) {
    if (sapply(DT, function(x) class(x)[1L]) == "haven_labelled") { 
        attr(DT[,i],"class[1L]") <- "integer"
    }
 }

编辑 2:答案在应用 plm 时防止了任何错误。遗憾的是,所有 coefficientsstandard errors 都为零。 P-valuest-valuesNA。我不确定是什么原因造成的。

此解决方案基于提供的数据集 DTp,根据您的原始数据集

更改 labelledlabelled_ch
for (i in seq_len(ncol(DTp))) {
  if (any(class(DTp[,i]) == "labelled")) {
    #browser()
    ind = which(class(DTp[,i])=="labelled")
    attr(DTp[,i],"class")[ind] <- "labelled_ch"
  }
}