'x' must be numeric histogram in R

走远了吗. 提交于 2021-02-08 07:21:22

问题


I have a dataset with five variables: Dataset, Biome, Species, Growth.form and N.content. I'm trying to make a histogram with the N.content variable only, but I'm getting the error:

Error in hist.default(Ndata, xlab = "Blader", ylab = "N.content", main = "N.content",  : 
'x' must be numeric

What am I doing wrong?

Here's my script:

mydata <- read.table("Leaf N content.txt", sep="\t", header=TRUE)
summary(mydata)
class(mydata)
str(mydata)
table(mydata$Growth.form)
table(mydata$Biome)
Sumdata <- as.data.frame(with(mydata, table(Biome, Growth.form))) 
table(Sumdata)
Ndata <- subset(mydata, select=c(N.content))
logdata <- log(Ndata)
par(mfrow=c(1,2)) 
hist(Ndata, xlab="Blader", ylab="N.content", main="N.content", col= "red")
hist(logdata, xlab="Blader", ylab="N.content", main="N.content", col= "red")

回答1:


mydata is a data.frame. subset(mydata, select=c(N.content)) returns a data.frame. hist expects a (numeric) vector. Use Ndata <- mydata$N.content to select a column vector.



来源:https://stackoverflow.com/questions/33347560/x-must-be-numeric-histogram-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!