我正在使用gapminder数据集,并且在尝试对总体进行求和时,整数变量,错误
library(gapminder)
gapminder %>%
group_by(year) %>%
summarise(totalPop = sum(pop))
显示NA作为结果
然后,如果尝试
mean(gapminder$pop)
[1] 29601212 # ok, as expected
但是如果
sum(gapminder$pop)
integer overflow - use sum(as.numeric(.))[1] NA
我读了What is integer overflow in R and how can it happen?
所以,我试过
sum(gapminder$pop / 1)
[1] 50440465801
sum 的任何解释都不起作用,但是 sum / 1 呢?
简而言之:为什么?
gapminder %>%
group_by(year) %>%
summarise(totalPop = sum(pop))
但
gapminder %>%
group_by(year) %>%
summarise(totalPop = sum(pop/1))
答案 0 :(得分:2)
除以1强制数字。
library(gapminder)
class(sum(gapminder$pop))
[1] "integer"
Warning message:
In sum(gapminder$pop) : integer overflow - use sum(as.numeric(.))
class(sum(gapminder$pop/1))
[1] "numeric"