I've got a data frame df
given by:
r t s v
1 1 a 4.50
2 1 b 3.00
3 2 c 3.22
4 3 d 2.00
5 3 a 5.00
6 1 c 1.00
7 1 f 14.00
8 2 b 144.00
9 3 c 2.00
10 4 a 22.00
11 2 a 2.20
12 3 e 232.00
13 4 g 45.00
14 3 g 4.30
15 3 b 3.20
16 4 b 2.00
17 4 c 2.60
and I want to convert this into another data frame df1
as
r t a b c d e f g
1 1 4.5 3.0 1.00 NA NA 14.0 NA
2 2 2.2 144.0 3.22 NA NA NA NA
3 3 5.0 3.2 2.00 2 232 NA 4.3
4 4 22.0 2.0 2.60 NA NA NA 45.0
where the colnames
in df1
are the unique values from s
column in df
and they are grouped by their occurrence in t
column in df.
There won’t be any duplicates of ‘s’ in each ‘t’ so it can be assumed that each ‘s’ only appears once for every ‘t’ value.
Is there an easy way (using dplyr
or similar) to manipulate the data in df
to get df1
?