2

I've got a data frame like this:

set.seed(100)

drugs <- data.frame(id = 1:5, 
                drug_1 = letters[1:5], drug_dos_1 = sample(100,5),
                drug_2 = letters[3:7], drug_dos_2 = sample(100,5)
)

id drug_1 drug_dos_1 drug_2 drug_dos_2
1      a         31      c         49
2      b         26      d         81
3      c         55      e         37
4      d          6      f         54
5      e         45      g         17

I'd like to transform this messy table into a tidy table with all drugs of an id in one column and the corresponding drug dosages in one column. The table should look like this in the end:

id drug dosage
1  a    31
1  c    49
2  b    26
2  d    81
etc

I guess this could be achieved by using a reshaping function that transforms by data from wide to long format but I didn't manage.

Roccer
  • 899
  • 2
  • 10
  • 25

2 Answers2

3

One option is melt from data.table which can take multiple patterns in the measure argument

library(data.table)
melt(setDT(drugs), measure = patterns('^drug_\\d+$', 'dos'),
     value.name = c('drug', 'dosage'))[, variable := NULL][order(id)]
#   id drug dosage
#1:  1    a     31
#2:  1    c     49
#3:  2    b     26
#4:  2    d     81
#5:  3    c     55
#6:  3    e     37
#7:  4    d      6
#8:  4    f     54
#9:  5    e     45
#10  5    g     17

Here, the 'drug' is common in all the columns, so we need to create a unique pattern. One way is to specify the starting location (^) followed by the 'drug' substring, then underscore (_) and one or more numbers (\\d+) at the end ($) of the string. For the 'dos', just use that substring to match those column names that have 'dos'

akrun
  • 874,273
  • 37
  • 540
  • 662
2
library(dplyr)
drugs %>% gather(key,val,-id) %>% mutate(key=gsub('_\\d','',key)) %>% #replace _1 and _2 at the end wiht nothing
          mutate(key=gsub('drug_','',key)) %>% group_by(key) %>%  #replace drug_ at the start of dos with nothin and gruop by key
          mutate(row=row_number()) %>% spread(key,val) %>%
          select(id,drug,dos,-row)


 # A tibble: 10 x 3
  id drug  dos  
  <int> <chr> <chr>
  1     1 a     31   
  2     1 c     49   
  3     2 b     26   
  4     2 d     81   
  5     3 c     55   
  6     3 e     37   
  7     4 d     6    
  8     4 f     54   
  9     5 e     45   
 10     5 g     17   
  Warning message:
  attributes are not identical across measure variables;
  they will be dropped
#This warning generated as we merged drug(chr) and dose(num) into one column (val) 
A. Suliman
  • 12,923
  • 5
  • 24
  • 37