I have the following data set:
Email Relationship Q1 Q2 Q3 Q4
1 sample@email.com Self 1 2 2 3
2 sample@email.com Peer 3 3 4 5
3 sample@email.com Peer 5 2 3 1
4 sample@email.com Peer 4 1 2 3
5 sample@email.com Peer 2 3 3 4
6 sample@email.com Direct Report 3 3 4 4
7 sample@email.com Direct Report 5 2 4 4
8 other@email.com Self 3 4 4 2
9 other@email.com Peer 2 2 3 4
10 other@email.com Peer 3 3 3 2
11 other@email.com Peer 2 5 5 3
12 other@email.com Direct Report 4 4 4 3
13 other@email.com Direct Report 5 3 2 1
14 other@email.com Direct Report 2 4 5 3
And I want to transform it from long to wide, such that I calculate means for each relationship group and total:
Email Q1-Overall Q1-Self Q1-Peer Q1-Direct Report Q2-Overall Q2-Self Q2-Peer Q2-Direct Report
other@email.com 3.00 3.00 2.33 3.67 3.57 4.00 3.33 3.67
sample@email.com 3.29 1.00 3.50 4.00 2.28 2.00 2.25 2.50
I've tried melting it:
df<-dcast(melt(Data_Long, id.vars=c("Email", "Relationship")), Email~Q1+Relationship)
And that gets me part of the way there, but my question is how to take it to the next step to get the means, or if there is a more efficient way. As my data has hundreds of questions, is there a way to efficiently convert all of it?
I've also tried the dplyr package summarise and spread commands, but wasn't able to find a way to combine the variables to create new variables in those. Any advice is appreciated.