0

I want to filter my table like that

input table:

x1    x2    x3
A    B     x 
A    B     y
A    B     z
C    D     u
C    D     v

output table:

x1    x2    x3
A     B     x,y,z
C     D     u,v
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ersan
  • 393
  • 1
  • 9

1 Answers1

1
dfout <- aggregate(x3 ~ x1 + x2, data = df, FUN = toString)

dfout
#   x1 x2      x3
# 1  A  B x, y, z
# 2  C  D    u, v

str(dfout)
# 'data.frame': 2 obs. of  3 variables:
#  $ x1: chr  "A" "C"
#  $ x2: chr  "B" "D"
#  $ x3: chr  "x, y, z" "u, v"

Reproducible data

df <- data.frame(
  x1 = c("A", "A", "A", "C", "C"), 
  x2 = c("B", "B", "B", "D", "D"), 
  x3 = c("x", "y", "z", "u", "v"),
  stringsAsFactors = FALSE
)
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • If you prefer a list column to a string column, change the `FUN` argument like so: `FUN = function(x) list(x)` – s_baldur Feb 09 '20 at 10:05