Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does wt in count() mean (R language)?

Tags:

r

count

dplyr

I have googled back and forth, but I can't seem to find a good explanation, to non native English speaker, of what does it mean? Please give me a concrete example with and without wt. Thank you

like image 837
Ift h Avatar asked Oct 18 '25 03:10

Ift h


1 Answers

wt stands for "weights".
The first example in help('count') that uses object df, is, in my opinion, very clear.

First, create the object.

library(dplyr)

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

1. Now, an example without wt.
As you can see from the data set above, there are

  1. 2 rows with gender == "female";
  2. 1 row with gender == "male".

And a non-weighted count will return those counts.

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

2. Now an example with weights, argument wt.

Suppose that in the original data there were 10 rows with males and 5 rows with females. All male rows were obtained from the same individual, "Max". And the female gender rows from two individuals, one row only for "Sandra" and 4 rows for "Susan".

Then the user aggregated the original, unprocessed data by name and the result was the data as posted. To get counts that account for the original, use a weighted count.
This is what the comment above the wt example says.

# 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
like image 194
Rui Barradas Avatar answered Oct 20 '25 17:10

Rui Barradas