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!
GET()
come from? – sjpis()
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 automatically – sjp