rpart “as.character(x) 中的错误:无法强制类型“builtin”是什么意思?到“字符”类型的向量; ”留言的意思是?
这几天我一直在与 rpart 进行斗争(试图为我拥有的这个数据集制作分类树),我认为现在是时候寻求生命线了:-)我确信这是我没有看到的愚蠢的事情,但这就是我一直在做的事情:
EuropeWater <- read.csv(file=paste("/Users/artessaniccola/Documents/",
"Magic Briefcase/CityTypology/Europe_water.csv",sep=""))
library(rpart)
attach(EuropeWater)
names(EuropeWater)
[1] "City" "waterpercapita_m3" "water_class" "population"
[5] "GDPpercapita" "area_km2" "populationdensity" "climate"
EuropeWater$water_class <- factor(EuropeWater$water_class, levels=1:3,
labels=c("Low", "Medium", "High"))
EuropeWater$climate <- factor(EuropeWater$climate, levels=2:4,
labels=c("Arid", "Warm temperate", "Snow"))
EuropeWater_tree <- rpart(EuropeWater$water_class ~
population+GDPpercapita + area_km2 + populationdensity +
EuropeWater$climate,
data=EuropeWater, method=class)
Error in as.character(x) :
cannot coerce type 'builtin' to vector of type 'character'
在我的一生中,我无法弄清楚错误是什么。
I've been banging my head against rpart
for a few days now (trying to make classification trees for this dataset that I have), and I think it's time to ask a lifeline at this point :-) I'm sure it's something silly that I'm not seeing, but here's what I've been doing:
EuropeWater <- read.csv(file=paste("/Users/artessaniccola/Documents/",
"Magic Briefcase/CityTypology/Europe_water.csv",sep=""))
library(rpart)
attach(EuropeWater)
names(EuropeWater)
[1] "City" "waterpercapita_m3" "water_class" "population"
[5] "GDPpercapita" "area_km2" "populationdensity" "climate"
EuropeWater$water_class <- factor(EuropeWater$water_class, levels=1:3,
labels=c("Low", "Medium", "High"))
EuropeWater$climate <- factor(EuropeWater$climate, levels=2:4,
labels=c("Arid", "Warm temperate", "Snow"))
EuropeWater_tree <- rpart(EuropeWater$water_class ~
population+GDPpercapita + area_km2 + populationdensity +
EuropeWater$climate,
data=EuropeWater, method=class)
Error in as.character(x) :
cannot coerce type 'builtin' to vector of type 'character'
and for the life of me, I can't figure out what the Error is about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效吗?
我认为你应该引用方法类型。
当您使用
class
而不是"class"
时,R 会尝试将其自身转换为字符:因为
class
是一个类型为builtin 的函数:
Does this work?
I think you should quote method type.
When you use
class
instead of"class"
then R is trying convert to character themself:Because
class
is a function with typebuiltin
:我首先修复公式:删除多余的
EuropeWater
因为您已经提供了data=
参数:另外,请确保
data.frame 的所有列
属于适当的类型。也许从 csv 文件读取的某些数据被错误地读取为一个因素?快速的摘要(EuropeWater)
可能会揭示这一点。I would start by fixing the formula: remove the redundant
EuropeWater
as you already supply thedata=
argument:Also, make sure that all columns of your
data.frame
are of the appropriate type. Maybe some of the data read from the csv file was mistakenly read as a factor? A quicksummary(EuropeWater)
may reveal this.