在R中融合具有多个测量变量和类别的anova形状数据集

时间:2017-05-31 14:18:46

标签: r data.table melt

我知道这个问题可能已经被问过了,但是我无法找到在多个测量变量存在的情况下我面临融化的具体问题。 我有这样的数据表。

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))

我需要做的就是按类别将变量RT和ecc放在两个单独的列中,并添加一个指定了类别的列:

 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

我尝试将reshapevarying功能一起使用,但没有成功。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您可以使用tidyr


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

答案 1 :(得分:2)

由于它是data.table,我们可以有效地使用data.table方法

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