How to convert dataframe with 3 columns into a matrix in R?
- cols - unique values of the first column
- rows - unique values of the second column
The first 2 columns are string and the 3rd column is numeric.
need to calculate the mean
of 3rd column according to the names matching
example
# My data frame as like follows
B=c("B1", "B1", "B1", "B1", "B2", "B2", "B2", "B2")
A=c("A1", "A1", "A2", "A2", "A1", "A1", "A2", "A2")
count=1:8
df = data.frame(B,A,count)
df
B A count
1 B1 A1 1
2 B1 A1 2
3 B1 A2 3
4 B1 A2 4
5 B2 A1 5
6 B2 A1 6
7 B2 A2 7
8 B2 A2 8
the converted matrix should like as follows. Values of the matrix should be the mean of according to
A1 A2
B1 1.5 3.5
B2 5.5 7.5
I have tried many ways but didn't work any.