Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape values from 1 column and attach to existing column name

Tags:

r

reshape

melt

I have a dataset that looks like this:

Col1     Col2    Col3   Col4    Col5
   A        1       1     10      90
   A        1       2     20     100 
   A        1       3     30     110 
   A        1       4     40     120
   B        2       1     50     130
   B        2       2     60     140 
   B        2       3     70     150
   B        2       4     80     160

How do I reshape this so that I have the value from Col1 be in the column name of all columns from Col4 and on? My actual dataset has like 20 columns.

I want my final output to look like this:

Col2    Col3   Col4_A    Col4_B      Col5_A      Col5_B
   1       1       10        NA          90          NA
   1       2       20        NA         100          NA
   1       3       30        NA         110          NA
   1       4       40        NA         120          NA 
   2       1       NA        50          NA         130
   2       2       NA        60          NA         140
   2       3       NA        70          NA         150
   2       4       NA        80          NA         160
like image 531
nak5120 Avatar asked Mar 06 '23 06:03

nak5120


1 Answers

We can use gather, unite and spread from tidyr:

library(dplyr)
library(tidyr)

df %>%
  gather(var, value, -(Col1:Col3)) %>%
  unite(var, var, Col1, sep="_") %>%
  spread(var, value)

Result:

  Col2 Col3 Col4_A Col4_B Col5_A Col5_B
1    1    1     10     NA     90     NA
2    1    2     20     NA    100     NA
3    1    3     30     NA    110     NA
4    1    4     40     NA    120     NA
5    2    1     NA     50     NA    130
6    2    2     NA     60     NA    140
7    2    3     NA     70     NA    150
8    2    4     NA     80     NA    160
like image 158
acylam Avatar answered Apr 07 '23 08:04

acylam