SteveC81
SteveC81

Reputation: 35

Non-numeric Argument to Binary Operator Error R Studio

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!

Upvotes: 0

Views: 1118

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388982

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

Upvotes: 2

pseudospin
pseudospin

Reputation: 2767

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)

Upvotes: 1

Related Questions