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,100 points
11 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,140 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Custom Function to replace missing values in a vector with the mean of values

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
31 views
+1 vote
1 answer

Finding number of missing values and removing those missing values from a data-frame

This code gives the total number of ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
19 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,040 points
37 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,680 points
31 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,680 points
18 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
27 views
0 votes
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,140 points
61 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.