Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

reshape

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?

like image 928
Harry Palmer Avatar asked Mar 08 '13 16:03

Harry Palmer


1 Answers

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
like image 117
Roland Avatar answered Oct 13 '22 01:10

Roland