Help needed: non-numeric argument to binary operator

I am not sure why this error keeps popping out

my data looks like this:
image

my code is as below:

boxplot(split(Figure1$opt-out rate,Figure1$Species))
Error in x[floor(d)] + x[ceiling(d)] :
non-numeric argument to binary operator

It is always a good idea to have a reproducible example. It helps us help you. Here you can read more about how to do it:

That being said, in your case I'm pretty sure the problem is that your column name (specifically, opt-out rate) is not a valid name, so R is getting confused.

To fix this, you can do the following:

  1. Change names of the column with, e.g., make.names function in base R.
  2. Use the name, but put it in backticks:
Figure1$`opt-out rate`
3 Likes

Did you paste the code exactly as you are running it? Because when I try to run the code (mocking up your data frame as best I could), I'm getting a different error due to the syntactically invalid name:

# Create some fake data with a similar format as the real data
Figure1 <- data.frame(
  Species = c(rep("Species1", 5), rep("Species2", 5)),
  `opt-out rate` = rbinom(10, 10, 0.5)/10,
  `Chosen-forced advantage` = c(rep(0.002, 5), rep(NA, 5)),
  check.names = FALSE
)

boxplot(split(Figure1$opt-out rate,Figure1$Species))
#> Error: <text>:9:31: unexpected symbol
#> 8: 
#> 9: boxplot(split(Figure1$opt-out rate
#>                                  ^

Created on 2018-07-21 by the reprex package (v0.2.0).

Have you taken a look at this community's recommendations for asking questions about R code? The example above shows a couple of strongly recommended practices: including sample data (screenshots can't be easily run!) and using the reprex package to format examples, which shows both the code and the output.

1 Like