I Have following data frame with one numeric variable and 2 characters. I want to show mean for numeric variable(z) for every combination of characters values. I used aggregate function to do it:
df <- data.frame(x=sample(c("a","b","c"),100,replace = T),
y=sample(c("A","B","C"),100, replace = T),
z=rnorm(100,2))
aggregate(df[,3],list(id1 = df[,1], id2 = df[,2]),mean)
Output looks like this:
id1 id2 x
1 a A 2.052119
2 b A 2.058046
3 c A 2.397236
4 a B 2.342341
5 b B 2.182605
6 c B 2.227108
7 a C 1.733620
8 b C 1.725497
9 c C 1.966901
I'd like to transform it to look like that, or use other function which will give output below:
a b c
A 2.052119 2.058046 2.397236
B 2.342341 2.182605 2.227108
C 1.733620 1.725497 1.966901