Мы можем сделать это с data.table
library(data.table)
setDT(df)[, .(new_B = toString(B), Repeat = first(Repeat)), A]
# A new_B Repeat
# 1: a x1, x5, x4, x2, x3 5
# 2: b x2, x4, x1 3
# 3: c x5, x9, x3 3
# 4: d x2, x8 2
# 5: e x5, x1 2
# 6: f x6 1
# 7: g x2 1
# 8: h x5 1
# 9: i x4 1
#10: j x7 1
данные
df <- structure(list(A = c("a", "a", "a", "a", "a", "b", "b", "b",
"c", "c", "c", "d", "d", "e", "e", "f", "g", "h", "i", "j"),
B = c("x1", "x5", "x4", "x2", "x3", "x2", "x4", "x1", "x5",
"x9", "x3", "x2", "x8", "x5", "x1", "x6", "x2", "x5", "x4",
"x7"), Repeat = c(5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L)),
class = "data.frame", row.names = c(NA,
-20L))