I am trying to learn how to "count by multiple groups" in R using the dplyr library. I generated some data, and now I want to count the number of people for each combination of city and country.
Can someone please tell me if the code I have written is correct?
library(dplyr)
Data_I_Have <- data.frame(
"Country" = c("USA", "USA", "USA", "SPAIN", "SPAIN", "SPAIN", "SPAIN", "SPAIN", "SPAIN", "FRANCE", "UK"),
"City" = c("Chicago", "Chicago", "Boston", "Madrid", "Madrid", "Madrid", "Barcelona", "Barcelona", "NA", "Paris", "London"),
" Person" = c("John", "John", "Jim", "Jeff", "Joseph", "Jason", "Justin", "Jake", "Joe", "Jaccob", "Jon")
)
summary = Data_I_Have %>%
dplyr::group_by(Country, City)%>%
dplyr::summarise(COUNT = n())
summary = data.frame(summary)
Suppose if I had wanted to count the number of distinct names, is this code correct?
summary_2 = Data_I_Have %>%
dplyr::group_by(Country,City)%>%
dplyr::summarise(UNIQUE_COUNT = n())
Is this correct as well?
Thanks