count()中的wt是什么意思(R语言)?

Ift*_*t h 4 r count dplyr

我在谷歌上来回搜索过,但对于非英语母语人士来说,我似乎找不到一个很好的解释,这是什么意思?请给我一个有和没有wt的具体例子。谢谢

Rui*_*das 8

wt代表"weights"
在我看来,第一个help('count') 使用 object 的df例子非常清楚。

首先,创建对象。

library(dplyr)

df <- tribble(
  ~name,    ~gender,   ~runs,
  "Max",    "male",       10,
  "Sandra", "female",      1,
  "Susan",  "female",      4
)
Run Code Online (Sandbox Code Playgroud)

1.现在,举一个没有wt.
从上面的数据集中可以看出,有

  1. 2 行带有gender == "female";
  2. 1 行与gender == "male".

非加权计数将返回这些计数。

# counts rows:
df %>% count(gender)
## A tibble: 2 x 2
#  gender     n
#  <chr>  <int>
#1 female     2
#2 male       1
Run Code Online (Sandbox Code Playgroud)

2.现在是一个带有权重、参数的示例wt

假设原始数据中有 10 行男性,5 行女性。所有雄性行均来自同一个体"Max"。女性性别行来自两个个体,一行仅用于"Sandra",四行用于"Susan"

然后,用户聚合原始的、未处理的数据name,结果就是发布的数据。要获取原始的计数,请使用加权计数。
这就是示例上面的注释wt所说的内容。

# use the `wt` argument to perform a weighted count. This is useful
# when the data has already been aggregated once
# counts runs:
df %>% count(gender, wt = runs)
## A tibble: 2 x 2
#  gender     n
#  <chr>  <dbl>
#1 female     5
#2 male      10
Run Code Online (Sandbox Code Playgroud)