1

I have a data set with the following structure:

df1 <- data.frame(
session = seq(1,10,1))

df2 <- data.frame(
session = rep(1:10, each=4),
sample = seq(1,40,1))

What I need returned is a df1 with the following structure:

session | sample.list
 1      |    1,2,3,4
 2      |    5,6,7,8
 3      |    9,10,11,12
...

Basically its lists all the samples taken within a given session.

Thanks for any pointers or solutions to solve this.

doncarlos
  • 401
  • 4
  • 16

1 Answers1

2

With base R

aggregate(sample ~ session, df2, toString) 

Or (depends on the type of the desired result)

aggregate(sample ~ session, df2, list)

Or using data.table

library(data.table)
setDT(df2)[, list(sample.list=toString(sample)), session]

Or if you need the column as list

setDT(df2)[, list(sample.list=list(sample)), session]

Update

For listing unique "samples"

 aggregate(sample ~ session, df2, function(x) toString(unique(x)))

Or

 setDT(df2)[, list(toString(unique(sample))), session]
akrun
  • 874,273
  • 37
  • 540
  • 662