2
votes

I know the question has probably already been asked, but I couldn't find instances of the specific problem I'm facing with melting at the presence of multiple measure vars. I have a data table like this.

library(data.table)
set.seed(234)
DT<-data.table(item=1:3,phase=c("pre-test","test","follow up"),
               control_RT=sample(400:600,3),control_ecc=sample(100:200,3),
               oa_RT=sample(500:700,3),oa_ecc=sample(200:250,3),ya_RT=sample(450:550,3),ya_ecc=sample(230:260,3))

All I need to do is to put the variables RT and ecc in two separate columns by category, and add a column in which the category is specified:

 item   phase       RT  ecc  Category
    1   pre-test    549 178 control
    2   test        556 106 control
    3   follow up   403 163 control
    1   pre-test    686 214 oa
    2   test        643 227 oa
    3   follow up   684 226 oa
    1   pre-test    508 243 ya
    2   test        550 239 ya
    3   follow up   450 251 ya

I tried to use reshape with varying function but had no success. Any advice?

2
have a look at tidyr function gatherNico Coallier

2 Answers

2
votes

You can use tidyr for this.


library(tidyr)

DT %>% 
  gather("key", "value", -item, -phase) %>% 
  separate(key, c("Category", "key")) %>% 
  spread(key, value)

#>   item     phase Category ecc  RT
#> 1    1  pre-test  control 178 549
#> 2    1  pre-test       oa 214 686
#> 3    1  pre-test       ya 243 508
#> 4    2      test  control 106 556
#> 5    2      test       oa 227 643
#> 6    2      test       ya 239 550
#> 7    3 follow up  control 163 403
#> 8    3 follow up       oa 226 684
#> 9    3 follow up       ya 251 450
2
votes

As it is a data.table, we can use data.table methods efficiently

library(data.table)
dcast(melt(DT, id.var = c('item', 'phase'))[, c('Category', 'key') := 
       tstrsplit(variable, '_')], item + phase + Category ~key, value.var = 'value')
#  item     phase Category  RT ecc
#1:    1  pre-test  control 549 178
#2:    1  pre-test       oa 686 214
#3:    1  pre-test       ya 508 243
#4:    2      test  control 556 106
#5:    2      test       oa 643 227
#6:    2      test       ya 550 239
#7:    3 follow up  control 403 163
#8:    3 follow up       oa 684 226
#9:    3 follow up       ya 450 251