This is an example for the data frame i have:
Location <- c("a", "a", "a", "b", "b", "b", "b")
Code <- c("70", "70", "60", "60", "60", "60", "50")
df <- data.frame(Location, Code)
I want to group and summarize the codes using this code:
df2 <- df %>%
dplyr::group_by(df$Code) %>%
dplyr::summarize(Number = n()) %>%
dplyr::mutate(Percent = round((Number/sum(Number)*100))) %>%
dplyr::arrange(desc(Number))
I want this for every location though. And to add some difficutly; it is not clear, if there are different locations beforehand. The goal is to have an automatic function, that gives me the result for every location, by just using different data frames, where it is not clear, which locations are in it.
The result i am looking for is df2. df2 is for all locations though. i want a data frame, where the codes are listed and summarized by each location. so location "a" would have the codes 70 (2 times) and 60 (1 time) and the location "b" has 60 (3 times) and 50 (1 time).
I can not find a solution for this (only if i do it step by step, which i dont want)
Thanks in advance.
$indplyrcode. What is expected output that you are looking for? Do you want to addLocationingroup_bylikedplyr::group_by(Location, Code)? - Ronak Shah