I have a data frame with these values:
X1 X2 X3
s1 45.11 1
s1 45.13 1
s1 53.42 2
s1 51.41 2
s2 96.76 3
s2 96.65 3
s4 77.9 4
s1 80.46 5
s3 43.58 2
s1 43.12 2
s1 41.51 3
s4 41.97 3
s1 108.97 6
s3 117.46 6
s4 40 3
s4 40 3
s5 25.4 1
s5 25.5 1
I want to convert it to a data frame in this kind of format:
s1 s2 s3 s4 s5
1 45.12 0 0 0 25.45
2 49.32 0 43.58 0 0
In this, the values are the mean values of the X2
column above that match the criteria, i.e., are part of row s1
and have the value of X3
as 1.
How can I achieve this in R?