bearbjorn
bearbjorn

Reputation: 13

'x' must be numeric histogram in R

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")

Upvotes: 1

Views: 14459

Answers (1)

Roland
Roland

Reputation: 132864

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.

Upvotes: 4

Related Questions