Search code examples
rdata-management

How to combine columns based on another column


I have a data frame similar to this one. Where each 'id' has values connected to three groups. Right now there are 15 rows and 3 columns.

id <- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5)
group <- c('Group1','Group2', 'Group3','Group1','Group2', 'Group3','Group1','Group2', 'Group3','Group1','Group2', 'Group3','Group1','Group2', 'Group3')
value <- c(49, 76, 14, 97, 78, 51, 48, 44, 81, 74, 85, 11, 82, 63, 91)

df <- cbind(id,group,value)

This gives the following data frame.

      id  group    value
 [1,] "1" "Group1" "49" 
 [2,] "1" "Group2" "76" 
 [3,] "1" "Group3" "14" 
 [4,] "2" "Group1" "97" 
 [5,] "2" "Group2" "78" 
 [6,] "2" "Group3" "51" 
 [7,] "3" "Group1" "48" 
 [8,] "3" "Group2" "44" 
 [9,] "3" "Group3" "81" 
[10,] "4" "Group1" "74" 
[11,] "4" "Group2" "85" 
[12,] "4" "Group3" "11" 
[13,] "5" "Group1" "82" 
[14,] "5" "Group2" "63" 
[15,] "5" "Group3" "91"

I want to reshape the data set so it has 5 rows and 4 columns. One row for each unique 'id' and one column for each group's value. The desired outcome looks like this:

     id Group1_value Group2_value Group3_value
[1,]  1           49           76           14
[2,]  2           97           78           51
[3,]  3           48           44           81
[4,]  4           74           85           11
[5,]  5           82           63           91

Thanks in advance!


Solution

  • Here is a base R solution:

    reshape(as.data.frame(df), v.names = "value", idvar = "id", 
            timevar = "group", direction = "wide")
    
    #>    id value.Group1 value.Group2 value.Group3
    #> 1   1           49           76           14
    #> 4   2           97           78           51
    #> 7   3           48           44           81
    #> 10  4           74           85           11
    #> 13  5           82           63           91