Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从数据集/变量中删除自定义(第二)类_R_Class_Plm_Hmisc - Fatal编程技术网

R 从数据集/变量中删除自定义(第二)类

R 从数据集/变量中删除自定义(第二)类,r,class,plm,hmisc,R,Class,Plm,Hmisc,我一直在使用hmisc包中的一个类,名为haven\u labeled(有时只是labeled)。其目的是从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(actualda

我一直在使用
hmisc
包中的一个类,名为
haven\u labeled
(有时只是
labeled
)。其目的是从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"
for for (i in seq_len(ncol(DT)) {
    if (sapply(DT, function(x) class(x)[1L]) == "haven_labelled") { 
        attr(DT[,i],"class[1L]") <- "integer"
    }
 }
因此,我希望从此数据库中删除标记为的类。很遗憾,我无法重现这个错误。我认为这与我的
actualdataset
中的
var
有关,它是一个双重类别,包括have
haven\u标签
。请参见以下示例数据集

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                                                                            # https://stackoverflow.com/questions/11036989/replace-all-0-values-to-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" 
库(data.table)
图书馆(plm)
图书馆(Hmisc)
种子(1)

DT此解决方案基于提供的数据集DTp,根据原始数据集更改
标记的
标记的

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"
  }
}
用于(序列中的i(ncol(DTp))){
如果(任何(类(DTp[,i])=“标记”)){
#浏览器()
ind=哪个(类别(DTp[,i])==“标记”)

attr(DTp[,i],“class”)[ind]如果运行:
class(DTp$Income)检查
attributes(DTp$Income)
attributes(DTp$Income)$class
attr(DTp$Income,“class”)
,在这种情况下,
attr(DTp$Income,“class”)@A.Suliman感谢您的评论。我正在寻找一个更通用的解决方案,可以用于整个(实际的)项目数据集。我编辑了原始帖子,以便更好地解释我希望看到的内容。你介意看一看吗?@Ben Nutzer感谢你的评论。我尝试了你的方法处理整个数据集,但将其转换为pseries列表。非常感谢!我现在顺利通过了
plm
阶段。请看第二次编辑。我不确定它是否可靠ted回答了最初的问题,这就是我删除它的原因。
lm
仍然有效。。啊,很高兴知道!对于示例数据集,我不确定它是否有那么令人惊讶,因为我没有对数据进行任何思考。但是,我现在能够通过
plm
和实际数据集得到结果。我认为我必须更加努力小心我在回归中所做的。非常感谢你的帮助!