powered by
Add a count column to the data frame.
df %>% add_count(a, b) is equivalent to using df %>% mutate(n = n(), .by = c(a, b))
df %>% add_count(a, b)
df %>% mutate(n = n(), .by = c(a, b))
add_count.(.df, ..., wt = NULL, sort = FALSE, name = NULL)
A data.frame or data.table
Columns to group by. tidyselect compatible.
tidyselect
Frequency weights. Can be NULL or a variable:
NULL
If NULL (the default), counts the number of rows in each group.
If a variable, computes sum(wt) for each group.
sum(wt)
If TRUE, will show the largest groups at the top.
TRUE
The name of the new column in the output.
If omitted, it will default to n.
n
df <- data.table( a = c("a", "a", "b"), b = 1:3 ) df %>% add_count(a)
Run the code above in your browser using DataLab