tell us
count() allows you to quickly count the number of unique values of one or more variables. While it seems similar to tally(), tally() only allows you to count within an already defined group, while count() allows you to define and group variables within the function.
show us
write a little demo to show how to install/load the package and use the function on some real data ### install and load packages
library(tidyverse)## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.2 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(palmerpenguins)get some data
data <- penguinsuse the function
Here is an example showing how count() allows you to group the data within the function, whereas with tally() you need to group your variables separately.
count(data, species)## # A tibble: 3 x 2
## species n
## <fct> <int>
## 1 Adelie 152
## 2 Chinstrap 68
## 3 Gentoo 124
data %>% count(species, sort = TRUE)## # A tibble: 3 x 2
## species n
## <fct> <int>
## 1 Adelie 152
## 2 Gentoo 124
## 3 Chinstrap 68
tally(data)## # A tibble: 1 x 1
## n
## <int>
## 1 344
data <- data %>%
group_by(species)
tally(data, sort = TRUE)## # A tibble: 3 x 2
## species n
## <fct> <int>
## 1 Adelie 152
## 2 Gentoo 124
## 3 Chinstrap 68
more resources
write a little paragraph about how you learned about the function- what did you google? Include a list of the documentation that you found useful andresources that someone learning about the function might need. If you can find pictures or memes to include, great!!
count() is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()). count() is paired with tally(), a lower-level helper that is equivalent to df %>% summarise(n = n()). Supply wt to perform weighted counts, switching the summary from n = n() to n = sum(wt). Documentation here