数据:
names real_name type
a A AA
aa A AA
a- A AA
b B BB
bbb B BB
...
通缉结果:
names real_name type
{a, aa, a-} A AA
{b, bbb} B BB
我想摆脱冗余并能够遍历名称。我几乎设法通过循环和tidyr
来获得想要的结果。
得到这个的优雅方法是什么?
答案 0 :(得分:4)
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)))
如果您需要list-col
,可以尝试nest
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]>
或summarize
如果你真的想要一个包含所有名字的字符串:
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