How can I get lists of rownames as values after aggregating a dataframe?

r, reshape

Solution

library(reshape2)
df <- read.table(text="type    V1    V2
1    A    bla    bla
2    A    bla    bla
3    B    bloo    bla
4    B    bloo    bla
5    C    moo    bloo
6    C    moo    bloo")

df$id <- rownames(df)

melted <- melt(df, id=c('type','id'),measure.vars=c("V1","V2"))

fun <- function(x) paste(unique(x),collapse=",") 

dcast(melted,type~value,fun,value.var="id",fill="0")

#  type bla bloo moo
#1    A 1,2    0   0
#2    B 3,4  3,4   0
#3    C   0  5,6 5,6

Problem

I have a dataframe df like this: ``` type V1 V2 1 A bla bla 2 A bla bla 3 B bloo bla 4 B bloo bla 5 C moo bloo 6 C moo bloo ``` Currently I melt/cast to get this: ``` type bla bloo moo A 4 0 0 B 2 2 0 C 0 2 2 ``` using these commands: ``` library(reshape) melted <- melt(df, id='type') count <- function(x) { length(na.omit(x)) } casted <- cast(melted, type~value, count) ``` However instead of counting/summing, I want to get a list of the original rownames. Something like this: ``` type bla bloo moo A 1,2 0 0 B 3,4 3,4 0 C 0 5,6 5,6 ``` where each value for bla, bloo and moo is a list of unique rownames from the original data frame. Can anyone help?

Original source