-2

this sounds pretty basic but every time I try to make a histogram, my code is saying x needs to be numeric. I've been looking everywhere but can't find one relating to my problem. I have data with 240 obs with 5 variables.

Nipper length
Number of Whiskers
Crab Carapace
Sex
Estuary location

There is 3 locations and i'm trying to make a histogram with nipper length

I've tried making new factors and levels, with the 80 obs in each location but its not working

Crabs.data <-read.table(pipe("pbpaste"),header = FALSE)##Mac
names(Crabs.data)<-c("Crab Identification","Estuary Location","Sex","Crab Carapace","Length of Nipper","Number of Whiskers")
Crabs.data<-Crabs.data[,-1]
attach(Crabs.data)
hist(`Length of Nipper`~`Estuary Location`)

Error in hist.default(Length of Nipper ~ Estuary Location) : 'x' must be numeric

Instead of correct result

jogo
  • 12,469
  • 11
  • 37
  • 42
Jay
  • 1
  • 2
    *"Needs to be numeric"* typically points to the data being read in incorrectly. If you look at `sapply(x, class)`, you may find that columns you assumed were numeric are actually `factor` (or possibly `character`). The use of `read.table(pipe(...),...)` is likely going to complicate things, as we will not be able to reproduce any of your problems. I encourage you to formalize things a little by reading your data from a file (csv or xlsx), controlling your column classes (data types). – r2evans May 04 '19 at 08:03
  • 2
    `fortunes::fortune(379)` – jogo May 04 '19 at 08:15
  • 2
    I think that this is an example where the error message is telling you exactly what the problem is. That is the x variable in your formula is not numeric. What are you trying to accomplish by using formula notation? ?hist only mentions x as a numeric variable, not the use of a formula. So x must be a numeric variable. You can do class(Crabs.data$`Length of Nipper`) to check the class. Meantime as mentioned it's not really good practice to use `attach()` because it will end up causing other issues. – Elin May 04 '19 at 10:15

1 Answers1

0

hist() doesn't seem to like taking more than one variable.

I think you'd have the best luck subsetting the data, that is, making a vector of nipper lengths for all crabs in a given estuary.

crabs.data<-read.table("whatever you're calling it")
names<-(as you have it)

Estuary1<-as.vector(unlist(subset(crabs.data, `Estuary Loc`=="Location", select = `Length of Nipper`)))
hist(Estuary1)

Repeat the last two lines for your other two estuaries. You may not need the unlist() command, depending on your table. I've tended to need it for Excel files, but I don't know what format your table is in (that would've been helpful).