0
votes

I'm getting a Non-numeric Argument to Binary Operator Error in R when I try to divide one number by another in R. The code is below. Both columns in the dataframe are numeric integers when I run sapply (class and mode). Any suggestions would be very welcome.

##Get the Data for Businesses
Busi_URL <- GET("https://www.nomisweb.co.uk/api/v01/dataset/NM_142_1.data.json?geography=1820327937&industry=37748736&employment_sizeband=0&legal_status=0&measures=20100")

##Convert the Data 
Busi_Text <- content(Busi_URL, "text")
Busi_JSN <- fromJSON(Busi_Text, flatten = TRUE)
Busi_df <- as.data.frame(Busi_JSN)


##Get the Population Data
BusiPop_Url <-GET("https://www.nomisweb.co.uk/api/v01/dataset/NM_31_1.data.json?geography=1811939329&date=latestMINUS9-latest&sex=7&age=0&measures=20100")

BusiPop_Text <- content(BusiPop_Url, "text")
BusiPop_JSN <- fromJSON(BusiPop_Text, flatten = TRUE)
BusiPop_df <- as.data.frame(BusiPop_JSN)


##Join the tables together
FinalBusi_df <-full_join(x=Busi_df,
                         y=BusiPop_df,
                         by=c("obs.geography.geogcode","obs.time.value"))
##Ensure all are numeric
FinalBusi_df$obs.obs_value.value.x<-lapply(FinalBusi_df$obs.obs_value.value.x, as.numeric)
FinalBusi_df$obs.obs_value.value.y<-lapply(FinalBusi_df$obs.obs_value.value.y, as.numeric)

##Test
sapply(FinalBusi_df$obs.obs_value.value.x, class)
sapply(FinalBusi_df$obs.obs_value.value.y, class)
sapply(FinalBusi_df$obs.obs_value.value.x, mode)
sapply(FinalBusi_df$obs.obs_value.value.y, mode)

## Try the maths
FinalBusi_df %>%
  select(obs.obs_value.value.x, obs.obs_value.value.y, obs.time.value, obs.geography.description.x)%>%
  summarise(obs.obs_value.value.x/obs.obs_value.value.y)

Apologies if I'm missing or doing something completely obvious here, I've only been learning R for about a month or so, so I might be making a very rookie mistake!

2
Also, I know the joined table is a bit messy, I've not got round to filtering it down, but I don't want to do that until I've worked out how to process the maths on the two columns.SteveC81
where does the function GET() come from?sjp
can you try running is() on all of your variables? This error typically comes up when a variable that you think is numeric has been coded in as a character by R automaticallysjp
GET, comes from the httr package I believe. the other main package is jsonlite to mangle the incoming json from the nomis website.SteveC81
when I ran the 'is()' function, both come back as list vectorSteveC81

2 Answers

2
votes

Your issue is in this line :

##Ensure all are numeric
FinalBusi_df$obs.obs_value.value.x<-lapply(FinalBusi_df$obs.obs_value.value.x, as.numeric)
FinalBusi_df$obs.obs_value.value.y<-lapply(FinalBusi_df$obs.obs_value.value.y, as.numeric)

as.numeric is vectorised so you don't need lapply to turn them to numeric. Besides, when you use lapply the output returned is a list and hence you get an error Non-numeric Argument to Binary Operator. Change the above line to :

FinalBusi_df$obs.obs_value.value.x <- as.numeric(FinalBusi_df$obs.obs_value.value.x)
FinalBusi_df$obs.obs_value.value.y <- as.numeric(FinalBusi_df$obs.obs_value.value.y)

and then run your code and it works as expected.

library(dplyr)
FinalBusi_df %>% summarise(result = obs.obs_value.value.x/obs.obs_value.value.y)

#       result
#1          NA
#2  0.02395833
#3  0.02488152
#4  0.02492904
#5  0.02535411
#6  0.02849057
#7  0.02916275
#8  0.03024459
#9  0.02969043
#10 0.03300562
1
votes

Just look at the data. You've got missing numbers in the first row of the BusiPop table.

BusiPop_df %>% select(obs.obs_value.value, obs.obs_status.description)