我正在尝试计算每个国家/地区的预期寿命增加量,但我无法让 diff() 函数在这里工作。我还尝试了滞后/超前功能,但我无法使用它。任何建议表示赞赏。
\nlibrary(gapminder)\ngm <- gapminder\ninclife <- gm %>%\n drop_na(lifeExp)%>%\n group_by(country) %>%\n select(country, year, lifeExp) %>%\n mutate(inc = diff(lifeExp))\nRun Code Online (Sandbox Code Playgroud)\n这是我得到的错误
\nError in `mutate()`:\n! Problem while computing `inc = diff(lifeExp)`.\n\xe2\x9c\x96 `inc` must be size 12 or 1, not 11.\n\xe2\x84\xb9 The error occurred in group 1: country = "Afghanistan".\nRun `rlang::last_error()` to see where the error occurred.\nRun Code Online (Sandbox Code Playgroud)\n
追加一个NA或另一个值,因为diff将提供长度为 n-1 的向量,并且mutate要求观测值数量不变。
inclife <- gm %>%
drop_na(lifeExp)%>%
group_by(country) %>%
select(country, year, lifeExp) %>%
mutate(inc = c(NA,diff(lifeExp)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |