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)