Why it keep saying non-numeric argument to binary operator

I need to find the difference between '2016' and '2018' but it doesn't work. Because I cannot reach the website so I downloaded it. You may work with it as I put the original data at the top.

knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
energy_types <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/energy_types.csv")
#> Error in open.connection(structure(4L, class = c("curl", "connection"), conn_id = <pointer: 0x000000000000024b>), : Could not resolve host: raw.githubusercontent.com
country_totals <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/country_totals.csv")
#> Error in open.connection(structure(5L, class = c("curl", "connection"), conn_id = <pointer: 0x000000000000024d>), : Could not resolve host: raw.githubusercontent.com

energy_types_filter <- energy_types %>%
  filter(type == "Conventional thermal") 
#> Error in filter(., type == "Conventional thermal"): 找不到对象'energy_types'
energy_types_filter %>%
  mutate(change = diff('2016', '2018'))
#> Error in mutate(., change = diff("2016", "2018")): 找不到对象'energy_types_filter'
arrange(energy_types_filter, desc(change))
#> Error in arrange(energy_types_filter, desc(change)): 找不到对象'energy_types_filter'
mutate(energy_types_filter, country_name = fct_inorder(country_name))
#> Error in mutate(energy_types_filter, country_name = fct_inorder(country_name)): 找不到对象'energy_types_filter'

gg_energy_types_filter <- ggplot(
  energy_types_filter,
  aes(x = 0, xend = change, y = country_name, yend = country_name), color = "blue"
) +
  geom_segment(arrow = grid::arrow(length = unit(1, "mm"), type = "closed"), color = 0.9) +
  theme(axis.title.y = element_blank()) +
  geom_text(color = c("firebrick", "steelblue"))
#> Error in ggplot(energy_types_filter, aes(x = 0, xend = change, y = country_name, : 找不到对象'energy_types_filter'
  theme_minimal_vgrid() +
  theme(legend = element_blank())
#> Error in theme_minimal_vgrid(): 没有"theme_minimal_vgrid"这个函数

How about this;

library(tidyverse)
energy_types <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/energy_types.csv")
country_totals <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/country_totals.csv")

typeof(energy_types) ; typeof(country_totals)
#> [1] "list"
#> [1] "list"

Data is in a list format, Convert them to tibble format

energy_types <- as_tibble(energy_types)
country_totals <- as_tibble(country_totals)

Manipulate data

energy_types_filter <- energy_types %>%
filter(type == "Conventional thermal",) %>%
mutate( 'change' = 2018 - 2016,
country_name = fct_inorder(country_name) ) %>%
arrange(desc(change) )

Go to plot

gg_energy_types_filter <- ggplot(
energy_types_filter,
aes( x = 0, xend = change, y = country_name, yend = country_name), color = "blue"
) +
geom_segment(arrow = grid::arrow(length = unit(1, "mm"), type = "closed"), color = 0.9) +
geom_text( color = c("firebrick", "steelblue") ) + Add the label argument
cowplot::theme_minimal_vgrid() +
theme( axis.title.y = element_blank() )
gg_energy_types_filter

Well, in this case, I will only get "2" for the "change" variable. I need to subtract the 2016 column and the 2018 column from the dataset. I tried to add " in 2016 and 2018 but will only result

Error: Problem with `mutate()` column `change`.
i `change = "2018" - "2016"`.
x non-numeric argument to binary operator

The answer is in the error message. It is clearly shown that 2018 and 2016 are stored as character variables. Convert them to numeric and the arithmetic operator will work.

Try these codes below;

library(tidyverse)
energy_types <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/energy_types.csv")
country_totals <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/country_totals.csv")

# Check data format
typeof(energy_types) ; typeof(country_totals)
#> [1] "list"
#> [1] "list"

# Data is in a list format, Convert them to tibble format
energy_types <- as_tibble(energy_types)
country_totals <- as_tibble(country_totals)

# Manipulate data
energy_types_filter <- energy_types %>%
  filter(type == "Conventional thermal",) %>%
  mutate( change = `2018` - `2016`,
          country_name = fct_inorder(country_name) ) %>%
  arrange(desc(change) )

# Go to plot
gg_energy_types_filter <- ggplot(
  energy_types_filter,
  aes( x = 0, xend = change, y = country_name, yend = country_name), color = "blue"
) +
  geom_segment(arrow = grid::arrow(length = unit(1, "mm"), type = "closed"), color = 0.9) +
  #geom_text( color = c("firebrick", "steelblue") ) + Add the label argument
  cowplot::theme_minimal_vgrid() +
  theme( axis.title.y = element_blank() )
gg_energy_types_filter

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.