How to get lists of growth names as values ​​after aggregating a data frame?

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 

I am currently melting / casting to get this:

 type bla bloo moo A 4 0 0 B 2 2 0 C 0 2 2 

using the following 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 names of the growths. Something like that:

 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 growth names from the original data frame.

Can anyone help?

+8
r reshape
source share
1 answer
 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 
+11
source share

All Articles