【问题标题】:Error: Problem with `mutate()` input `gap`. x non-numeric argument to binary operator i Input错误:`mutate()` 输入 `gap` 有问题。 x 二元运算符 i 输入的非数字参数
【发布时间】:2020-10-15 08:45:50
【问题描述】:

我是 R 新手,正在使用 gapminder 数据集来尝试一些绘图。

这个数据集是从gapminder website 下载的最新数据,它返回一个error on mutate,而gapminder 来自library(gapminder) 的数据没有。

df:gapminder_new

gapminder_new %>%  
  select(country, "2010", "2019") %>%  head()

############## output ################
country 2010  2019
<chr>  <dbl>  <dbl>

Afghanistan 543 571     
Albania 4090    5210        
Algeria 4480    4710        
Andorra 40900   45900       
Angola  3590    3100        
Antigua and Barbuda 13000   15700   
 

错误:gapminder_newdf 中减去年份的列值时出错:

gapminder_new %>%  
  select(country, "2010", "2019") %>% 
  mutate(gap = "2019" - "2010") %>% head() 

Error: Problem with `mutate()` input `gap`. x non-numeric argument to binary operator i Input `gap` is `"2019" - "2010"`. Run `rlang::last_error()` to see where the error occurred.

但奇怪的是,当我从 libraryshaped to similar wider format 使用 gapminder data 时,这个没有问题:

df: library(gapminder) 然后就可以了

library(gapminder)

gapminder %>%
  filter(year == 1967 | year == 2007) %>%
  select(country, year, lifeExp) %>%
  spread(year, lifeExp) %>%
  mutate(gap = `2007` - `1967`) %>% head()

#################### output #########################

country 1967  2007 gap
<fctr>  <dbl> <dbl> <dbl>

Afghanistan 34.020  43.828  9.808   
Albania 66.220  76.423  10.203  
Algeria 51.407  72.301  20.894
Angola  35.985  42.731  6.746   
Argentina   65.634  75.320  9.686

两个数据集看起来完全相同,我试图在所有情况下都采用dbl 的年份差异,但它在一种情况下有效,在其他情况下无效

这里发生了什么,我哪里出错了,我该如何解决?

有时这些在 R 数据帧中看起来相同的数据类型返回的小问题确实令人沮丧。

从 excel 迁移到 python 并不难,就像从 Python 迁移到 R 一样。

【问题讨论】:

    标签: r dataframe tidyverse


    【解决方案1】:

    它现在已经使用 mutate(gap = .[["2019"]] - .[["2010"]] ) %&gt;% head() 工作了,但我仍然不知道为什么这需要在 new gapminder dataset 中工作,因为我不需要将此表单与 library(gapminder) 数据集一起使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      相关资源
      最近更新 更多