Data:
names real_name type
a A AA
aa A AA
a- A AA
b B BB
bbb B BB
...
Wanted result:
names real_name type
{a, aa, a-} A AA
{b, bbb} B BB
I want to get rid of the redundancy and be able to traverse through names. I have almost managed to get to the wanted results with loops and tidyr
.
What is the elegant way to get this?
df <- tibble(names = c("a", "aa", "a-", "b", "bbb"),
real_name = c(rep("A", 3), rep("B", 2)),
type = c(rep("AA", 3), rep("BB", 2)))
You can try nest
if you want a list-col
df %>% nest(names)
# A tibble: 2 x 3
real_name type data
<chr> <chr> <list>
1 A AA <tibble [3 x 1]>
2 B BB <tibble [2 x 1]>
Or summarize
if you really want a string with all the names in it:
df %>% group_by(real_name, type) %>% summarize(x = paste(names, collapse = ", "))
# A tibble: 2 x 3
# Groups: real_name [?]
real_name type x
<chr> <chr> <chr>
1 A AA a, aa, a-
2 B BB b, bbb