How to count instances of duplicate characters within a string?

Viewed 558

I have a dataframe:

levels     counts
1, 2, 2        24
1, 2           20
1, 3, 3, 3     15
1, 3           10
1, 2, 3        25

I want to treat, for example, "1, 2, 2" and "1, 2" as the same thing. So, as long as there is a "1" and "2" without any other character, it will count as the level "1, 2". Here is the desired data frame:

levels     counts
  1, 2         44
  1, 3         25
  1, 2, 3      25

Here is code to reproduce the original data frame:

df <- data.frame(levels = c("1, 2, 2", "1, 2", "1, 3, 3, 3", "1, 3", "1, 2, 3"), 
                 counts = c(24, 20, 15, 10, 25))
df$levels <- as.character(df$levels)
2 Answers
Related