英文:
Cannot perform binary operators on dbl's even though they're numeric?
问题
这是我的 tibble。
只是试图找到人口中的计数(案例)的百分比。/
不应该在双精度数上起作用吗?
帮助。谢谢。
英文:
This is my tibble.
Simply trying to find the % of count (cases) in population. Shouldn't the '/' work on dbls?
Help. Thanks.
答案1
得分: 1
请考虑在您的问题中创建一个最小可复现示例,以便其他人更容易修改您的代码并发现错误。您的问题还会受益于更具体的描述。
从您的截图中汇总信息,我认为您想通过将一个现有列除以另一个现有列来创建一个新列。执行此操作的函数是mutate()
,而不是add_column()
:
library(dplyr)
df <- tibble(
country = "Australia",
count = 8,
population = 18.4
)
df %>% mutate(pct_pop = (count / 1e6) / population)
#> # A tibble: 1 × 4
#> country count population pct_pop
#> <chr> <dbl> <dbl> <dbl>
#> 1 Australia 8 18.4 0.000000435
创建于2023年02月19日,使用reprex v2.0.2
看起来有点奇怪的是您将案例除以一百万,而不是人口。请确保您没有犯错。
英文:
Please consider creating a minimal reproducible example in your question to make it easier for others to modify your code and spot errors. Your question would also benefit from being more specific.
Putting together the pieces from your screenshots, I assume you would like to create a new column by dividing an existing column by another existing column. The function to do this is mutate()
, not add_column()
:
library(dplyr)
df <- tibble(
country = "Australia",
count = 8,
population = 18.4
)
df |> mutate(pct_pop = (count / 1e6) / population)
#> # A tibble: 1 × 4
#> country count population pct_pop
#> <chr> <dbl> <dbl> <dbl>
#> 1 Australia 8 18.4 0.000000435
<sup>Created on 2023-02-19 with reprex v2.0.2</sup>
It looks a bit odd that you are dividing the cases by a million, rather than population. Make sure you didn't get that wrong.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论