错误:“mutate()"输入“gap"有问题.x 二元运算符的非数字参数 i 输入 [英] Error: Problem with `mutate()` input `gap`. x non-numeric argument to binary operator i Input
问题描述
我是 R 新手,正在使用 gapminder 数据集来尝试一些绘图.
这个数据集是从 gapminder 网站
下载的最新数据,它返回一个 error on mutate
,其中 gapminder
数据来自 library(gapminder)
没有.
df:gapminder_new
gapminder_new %>%选择(国家,2010",2019")%>% head()############## 输出 ################国家 2010 2019<chr><dbl><dbl>阿富汗 543 571阿尔巴尼亚 4090 5210阿尔及利亚 4480 4710安道尔 40900 45900安哥拉 3590 3100安提瓜和巴布达 13000 15700
错误:在 gapminder_new
df 中减去年份的列值时出错:
gapminder_new %>%选择(国家,2010",2019")%>%变异(gap =2019"-2010")%>%head()错误:mutate()"输入gap"有问题.x 二元运算符的非数字参数 i 输入gap"是2019";- 2010".运行 `rlang::last_error()` 以查看错误发生的位置.
但奇怪的是,当我使用 library
中的 gapminder data
和 shape 为类似的更宽格式
时,这个没有问题:
df:library(gapminder) 然后就可以了
库(gapminder)间隙管理器%>%过滤器(年份 == 1967 | 年份 == 2007)%>%选择(国家,年份,lifeExp)%>%传播(年,lifeExp)%>%变异(gap = `2007` - `1967`)%>% head()#################### 输出 #########################国家 1967 2007 差距<fctr><dbl><dbl><dbl>阿富汗 34.020 43.828 9.808阿尔巴尼亚 66.220 76.423 10.203阿尔及利亚 51.407 72.301 20.894安哥拉 35.985 42.731 6.746阿根廷 65.634 75.320 9.686
两个数据集看起来完全一样,我试图计算在所有情况下都是 dbl
的年份差异,但它在一种情况下有效,而在其他情况下无效.>
这里发生了什么,我哪里出错了,我该如何解决?
有时这些在 R 数据帧中看起来相同的数据类型返回的小问题真的令人沮丧.
从 excel 迁移到 Python 并不难,就像从 Python 迁移到 R 一样.
它现在可以使用 mutate(gap = .[["2019"]] - .[["2010"]] ) %>% head()
但我仍然不知道为什么这需要在 new gapminder dataset
中工作,因为我不需要将此表单与 library 一起使用(gapminder)
数据集
I am new in R and working with gapminder dataset to try out some plots.
This dataset is latest data downloaded from gapminder website
which returns me an error on mutate
where as gapminder
data from library(gapminder)
doesn't.
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
Error: getting an error on subtracting year's column values in gapminder_new
df:
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.
But strange thing is when I use gapminder data
from library
and shaped to similar wider format
then this one has no issues:
df: library(gapminder) then it works
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
Both dataset looks exactly same and I am trying to take difference of years which are dbl
in all cases but it works in one case and not in other.
What is going on here, where am I going wrong and how do I fix it?
Sometimes these small problems of data type returns which appears same visually in R dataframes are really frustrating.
It was not hard in moving from excel to python as it seems to be in moving from Python to R.
it has now worked using mutate(gap = .[["2019"]] - .[["2010"]] ) %>% head()
but I still don't know why this is required to work in new gapminder dataset
where as I didn't need to use this form with library(gapminder)
dataset
这篇关于错误:“mutate()"输入“gap"有问题.x 二元运算符的非数字参数 i 输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!