Get list of rownames as values after aggregating a dataframe

0 votes

Consider a data frame like this:

    type    Var1    Var2
1    A    hey      hey
2    A    hey      hey
3    B    hello    hey
4    B    hello    hey
5    C    hii      hello
6    C    hii      hello

I used melt/cast to get the below result

type    hey    hello    hii
A        4      0       0
B        2      2       0
C        0      2       2

Refer to my code below:

library(reshape)
melted <- melt(data, id='type')
count <- function(z) { 
  length(na.omit(z))
}
casted_data <- cast(melted, type~value, count)

Here the output generated is not what I want, I do not want to count/sum. Instead, I want to get a list of the original row names.

Below is my desired output:

type    hey    hello    hii
A       1,2      0       0
B       3,4    3,4       0
C        0     5,6      5,6

Where each value of hey, hello, hii is a list of unique row names from original data frame.

Can anyone help?

Apr 17, 2018 in Data Analytics by DataKing99
• 8,130 points
26 views

1 answer to this question.

0 votes

You can use reshape2 library:

library(reshape2)
data <- read.table(text="type    Var1    Var2
1    A    hey      hey
2    A    hey      hey
3    B    hello    hey
4    B    hello    hey
5    C    hii      hello
6    C    hii      hello")

data$id <- rownames(data)

melted <- melt(data, id=c('type','id'),measure.vars=c("Var1","Var2"))

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

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

#  type hey hello hii
#1    A 1,2    0   0
#2    B 3,4  3,4   0
#3    C   0  5,6  5,6
answered Apr 17, 2018 by darklord
• 6,170 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
2 answers
0 votes
3 answers

Code snippet to find number of null values in each column in a dataframe in R

colSums(is.na(data_frame_name)) will return you number of nulls ...READ MORE

answered Jul 5 in Data Analytics by sindhu
169 views
0 votes
1 answer

How to replace only set of values in a vector to different values in a dataframe?

If you have a condition using which ...READ MORE

answered Aug 19 in Data Analytics by anonymous
• 31,840 points
21 views
0 votes
1 answer

Reshape data from long to wide format

You can use the reshape function reshape(data, idvar ...READ MORE

answered Apr 17, 2018 in Data Analytics by nirvana
• 3,060 points
59 views
0 votes
1 answer

Reshape data from long to wide format in R

Use reshape function: reshape(dat1, idvar = "name", timevar = ...READ MORE

answered Jun 13, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
133 views
0 votes
1 answer

Reshape dataframe without “timevar” from long to wide format in R

Assuming that the data is in the ...READ MORE

answered Jun 13, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
64 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
56 views
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,170 points
190 views
0 votes
1 answer