Artist | Points |
---|---|
Notorious BIG | 34 |
Notorious BIG feat. blah blah | 42 |
2pac | 20 |
Dr.Dre feat. 2pac | 30 |
I would like to group and sum by Artist for a table to look like so:
Artist | Points |
---|---|
Notorious BIG | 76 |
2pac | 50 |
I think the best way to approach this is to know which are the ones you want to combine since problem occurs when rows have multiple commonalities i.e.
Artist | Points |
---|---|
2pac | 34 |
Dr. Dre feat. 2pac | 100 |
Dr. Dre | 80 |
You can specify the names of the rows you want to combine using the base R grepl
function, and the dplyr
functions, case_when
and summarise
.
library(dplyr)
df <- data.frame(Artist = c("Notorious BIG", "Notorious BIG feat. blah blah", "2pac", "Dr.Dre feat. 2pac"),
Points = c(34, 42, 20, 30))
df <- df %>%
dplyr::mutate(Artist = case_when(
grepl("2pac", Artist) ~ "2pac",
grepl("Notorious BIG", Artist) ~ "Notorious BIG"),
Artist = factor(Artist)) %>%
dplyr::group_by(Artist) %>%
dplyr::summarise(Points = sum(Points)) %>%
dplyr::ungroup()