不能对`dbl`执行二进制运算,尽管它们是数值?

huangapple go评论63阅读模式
英文:

Cannot perform binary operators on dbl's even though they're numeric?

问题

这是我的 tibble。

预览 tibble

只是试图找到人口中的计数(案例)的百分比。/ 不应该在双精度数上起作用吗?

我得到的错误

帮助。谢谢。

英文:

This is my tibble.

Preview of tibble

Simply trying to find the % of count (cases) in population. Shouldn't the '/' work on dbls?

Error I get.

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 &lt;- tibble(
  country = &quot;Australia&quot;, 
  count = 8,
  population = 18.4
)

df |&gt; mutate(pct_pop = (count / 1e6) / population)
#&gt; # A tibble: 1 &#215; 4
#&gt;   country   count population     pct_pop
#&gt;   &lt;chr&gt;     &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;
#&gt; 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.

huangapple
  • 本文由 发表于 2023年2月19日 18:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定