Barplot error non-numeric argument to binary operator

Hi all,

I'm working through the Coding Club's "Getting Started with R and RStudio," and naturally am pretty new to R, but although I thought I had followed all the steps, it's giving me an error code when I try to make the barplot: Error in -0.01 * height : non-numeric argument to binary operator

Here is my code:

head(edidiv) #Displays the first few rows
tail(edidiv) #Displays the last rows
str(edidiv) #Tells whether the variables are continuous, integers, categorical, or characters

head(edidiv$taxonGroup) #Displays the first few rows of this column only
class(edidiv$taxonGroup) #Tells you what type of variable we're dealing with: it's character now but we want it to be a factor
edidiv$taxonGroup <- as.factor(edidiv$taxonGroup) #as.factor turns whatever value inside into a factor

class(edidiv$taxonGroup) #Now a factor
dim(edidiv) #Displays number of rows and columns
summary(edidiv) #Gives you a summary of the data
summary(edidiv$taxonGroup) #Gives you a summary of that particular variable (column) in your dataset

#Calculating Species Richness

#First argument of function filter is the data frame, the second is the condition you want to filter
Beetle <- filter(edidiv, taxonGroup == "Beetle")
Bird <- filter(edidiv, taxonGroup == "Bird")
Butterfly <- filter(edidiv, taxonGroup == "Butterfly")
Dragonfly <- filter(edidiv, taxonGroup == "Dragonfly")
Flowering.Plants <- filter(edidiv, taxonGroup == "Flowering.Plants")
Fungus <- filter(edidiv, taxonGroup == "Fungus")
Hymenopteran <- filter(edidiv, taxonGroup == "Hymenopteran")
Lichen <- filter(edidiv, taxonGroup == "Lichen")
Liverwort <- filter(edidiv, taxonGroup == "Liverwort")
Mammal <- filter(edidiv, taxonGroup == "Mammal")
Mollusc <- filter(edidiv, taxonGroup == "Mollusc")

Beetle.species <- length(unique(Beetle$taxonName))
Bird.species <- length(unique(Bird$taxonName))
Butterfly.species <- length(unique(Butterfly$taxonName))
Dragonfly.species <- length(unique(Dragonfly$taxonName))
Flowering.Plants.species <- length(unique(Flowering.Plants$taxonName))
Fungus.species <- length(unique(Fungus$taxonName))
Hymenopteran.species <- length(unique(Hymenopteran$taxonName))
Lichen.species <- length(unique(Lichen$taxonName))
Liverwort.species <- length(unique(Liverwort$taxonName))
Mammal.species <- length(unique(Mammal$taxonName))
Mollusc.species <- length(unique(Mollusc$taxonName))

#Create a vector and plot it

#We are chaining together all the values; pay attention to the object names you have calculated and their order

biodiv <- c("Beetle.species", "Bird.species", "Butterfly.species", "Dragonfly.species", "Flowering.Plants.species", "Fungus.species", "Hymenopteran.species", "Lichen.species", "Liverwort.species", "Mammal.species", "Mollusc.species")

barplot(biodiv)

I read a several forum threads and tried using as.numeric (though I definitely could have used that wrong) and renaming them but I still get the same error. The only place I differed from the tutorial was naming my objects something that made more sense to me so I suspect it has to do with that but after going back and looking at the code I'm just not sure what. Any suggestions? (This is probably a very simple solution)

So it looks like in your biodiv object you have the names of the variables as strings. If you remove the quotation marks does it work?

i.e:

biodiv <- c(Beetle.species, Bird.species,  .... etc.

To add to this:

the reason it says non-numeric argument to binary operator is that barplot requires numeric data. When you put the name of a variable in quotations, R doesn't treat that as a variable anymore, it just treats it as the literal character string, so instead of trying to plot the values that are contained within the variable Beetle.species (i.e. the result of length(unique(Beetle$taxonName)), it is trying to plot the letters "Beetle.species". Obviously there's no way for it to do this, so it throws you an error saying that the data you've supplied is not numeric.

Yes that worked! And thank you so much for the explanation as well, that actually makes sense to me now.

No worries. Glad to help.

Just as a note, (I'm also new here so also need to work on this), typically when you post a question relating to R (or really any language), people like it when you post a reproducible example or reprex rather than the exact content of your script.

The reason is that, say for example I put your code above into my R session - it wouldn't run. I don't have access to the dataset edidiv so I wouldn't be able to test it out on my system. This is important as your problems get more complex.

There are instructions here for next time you need to ask a question :slight_smile: :

Oh, I did read over needing a reprex and thought I did that correctly, but now I see there's a lot more too it. Well thank you for explaining it! I'll read through those articles before I post again for sure.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.