0

I have a data frame with three columns containing the id, a feature name and the value. For id I have different features in different rows. Example:

df1  <– data.frame(id = c("A","A","A","B","B","B"), name = c("f1","f2","f3","f1","f2","f3"),value = c(1.1, 2.1, 3.1, 1.2,2.2, 3.2)))

#>   id    feature value
#> 1  A     f1      1.1
#> 2  A     f2      2.1
#> 3  A     f3      3.1
#> 4  B     f1      1.2
#> 5  B     f2      2.2
#> 6  B     f3      3.2

I want to rearrange the data and collect all observations (features) for one id in one row. My want my result to look like this:

#>   id  f1  f2  f3
#> 1  A 1.1 2.1 3.1
#> 2  B 1.2 2.2 3.2

If the data frame consists of only one id the data frame could be transposed using

df2 <-  t(df1)
df2 <- data.frame(df2) 
colnames(df2) <- df1$feature

Question: How can I transpose the data group wise to get the desired data format using dplyr? Any help is appreciated! Thx

0 Answers0