在 R 中用多个度量变量和类别融化方差分析形状的数据集
melt anova shaped dataset with multiple measure vars and categories in R
我知道这个问题可能已经被问过,但我找不到我在存在多个度量变量时遇到的融化问题的具体实例。
我有这样的数据table。
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
我尝试将 reshape
与 varying
函数一起使用,但没有成功。有什么建议吗?
您可以为此使用 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
因为它是一个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
我知道这个问题可能已经被问过,但我找不到我在存在多个度量变量时遇到的融化问题的具体实例。 我有这样的数据table。
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
我尝试将 reshape
与 varying
函数一起使用,但没有成功。有什么建议吗?
您可以为此使用 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
因为它是一个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