R 中 2x2x2 方差分析的结构数据

Structure data for 2x2x2 ANOVA in R

我有以下数据集,其中一个子集是:

structure(list(Sex = c("Male", "Male", "Female", "Male", "Male", "Male"
), AgeGroup = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Young", 
"Old"), class = "factor"), FPT_Values_LEFT = c(0.615962446, 
NA, 0.466983199, 0.607728601, 0.534221292, 0.577650726), FPT_Values_RIGHT = c(0.534368277, 
NA, 0.456424206, 0.589604795, 0.564736903, 0.484459132)), row.names = c(NA, 
6L), class = "data.frame")

所以:

     Sex AgeGroup FPT_Values_LEFT FPT_Values_RIGHT
1   Male      Old       0.6159624        0.5343683
2   Male      Old              NA               NA
3 Female      Old       0.4669832        0.4564242
4   Male      Old       0.6077286        0.5896048
5   Male      Old       0.5342213        0.5647369
6   Male      Old       0.5776507        0.4844591

我想 运行 AgeGroup*SIDE*value 的方差分析,但需要让数据集包含对应于 FPT_Values_LEFTFPT_Value_RIGHT 的侧栏。

我使用了 melt 的变体,但它没有准确地与值并排排列。

DF$SIDE <- c("Left", "Right") 

reshape::melt(id="SIDE")
or
reshape::melt(id=c("SIDE", "AgeGroup"))

reshape::melt(id=c("SIDE", "AgeGroup"), measure.vars = c("FPT_Value_LEFT", "FPT_Value_RIGHT"))

我知道我可以转置和附加数据以将正确的值移动到左侧下方,复制性别和年龄列以匹配,然后添加一个侧列。不过我不确定是否有更有效的方法。

预期输出:

   Sex AgeGroup FPT_Values         SIDE
1   Male      Old       0.6159624  Left
2   Male      Old              NA  Left
3 Female      Old       0.4669832  Left
4   Male      Old       0.6077286  Left
5   Male      Old       0.5342213  Left
6   Male      Old       0.5776507  Left
1   Male      Old     0.5343683    Right
2   Male      Old            NA    Right
3 Female      Old     0.4564242    Right
4   Male      Old     0.5896048    Right
5   Male      Old     0.5647369    Right
6   Male      Old     0.4844591    Right


使用data.table你可以做到:

> library(data.table)
> melt(as.data.table(DF), id = c("Sex", "AgeGroup"))[, variable := sub("FPT_Values_", "", variable)][]
       Sex AgeGroup variable     value
 1:   Male      Old     LEFT 0.6159624
 2:   Male      Old     LEFT        NA
 3: Female      Old     LEFT 0.4669832
 4:   Male      Old     LEFT 0.6077286
 5:   Male      Old     LEFT 0.5342213
 6:   Male      Old     LEFT 0.5776507
 7:   Male      Old    RIGHT 0.5343683
 8:   Male      Old    RIGHT        NA
 9: Female      Old    RIGHT 0.4564242
10:   Male      Old    RIGHT 0.5896048
11:   Male      Old    RIGHT 0.5647369
12:   Male      Old    RIGHT 0.4844591

如果您想继续使用“reshape2”中的 melt 而不是使用 data.table,您可以通过以下方式实现同​​样的效果:

transform(reshape2::melt(DF, c("Sex", "AgeGroup")), variable = sub("FPT_Values_", "", variable))

但是,我建议更新到 data.table 或将您的代码转换为使用 tidyr

使用tidyr::pivot_longer

tidyr::pivot_longer(df, 
                    cols = starts_with('FPT_Values'), 
                    names_to = c('.value', 'SIDE'), 
                    names_pattern = '(.*)_(\w+)')

#    Sex    AgeGroup SIDE  FPT_Values
#   <chr>  <fct>    <chr>      <dbl>
# 1 Male   Old      LEFT       0.616
# 2 Male   Old      RIGHT      0.534
# 3 Male   Old      LEFT      NA    
# 4 Male   Old      RIGHT     NA    
# 5 Female Old      LEFT       0.467
# 6 Female Old      RIGHT      0.456
# 7 Male   Old      LEFT       0.608
# 8 Male   Old      RIGHT      0.590
# 9 Male   Old      LEFT       0.534
#10 Male   Old      RIGHT      0.565
#11 Male   Old      LEFT       0.578
#12 Male   Old      RIGHT      0.484