首页 > 解决方案 > Format data to run ANOVA in R

问题描述

I am trying to run a 3-way ANOVA in R, but my values for each variable are in one column and not separated by rows. Currently, my data frame looks something like this:

Season  Site    Location    Replicate   Lengths
Jan_16  MI      Adj        1.00      ,
Jan_16  MI      Adj        2.00      ,
Jan_16  MI      Adj        3.00      ,
Jan_16  MI     Away        1.00      3,4,
Jan_16  MI     Away        2.00      ,
Jan_16  MI     Away        3.00      ,
Jan_16  MP     Adj         1.00      4,5,6,5,4,5,4,4,4,4,5,4,6,4,
Jan_16  MP     Adj         2.00      4,4,3,3,5,4,3,4,5,3,4,3,4,3,4,6,
Jan_16  MP     Adj         3.00      4,6,5,5,4,
Jan_16  MP     Away        1.00      ,4,4,10,4,5,4,6,5,5,
Jan_16  MP     Away        2.00       3,4,4,4,5,5,4,5,
Jan_16  MP     Away        3.00       4,4,13,4,

Lengths is the response variable that I wish to run the ANOVA on, how would I do this? Just a "," means there is no data.

**** EDIT

I have tried separate rows

library(tidyr)

separate_rows(data.frame, Season:Replicate, Lengths, convert=numeric )


#Error: All nested columns must have the same number of elements

The Lengths have a different number of variables, so is there a way to unnest this?

标签: rdataframestatisticscommaanova

解决方案


从您的问题中不清楚您的自变量是什么。在以下示例中,我假设Site,LocationReplicate您的 IV。

让我们首先将条目拆分Lengths为不同的行,并删除带有 missing/no的行Lengths

library(tidyverse)
df.aov <- df %>%
    mutate(Lengths = str_split(Lengths, ",")) %>%
    unnest() %>%
    filter(Lengths >= 0)

我们现在可以执行 3-way ANOVAaov

res <- aov(Lengths ~ Site * Location * Replicate, data = df.aov)
res
#Call:
#   aov(formula = Lengths ~ Site * Location * Replicate, data = df.aov)
#
#Terms:
#                     Site  Location Replicate Location:Replicate Residuals
#Sum of Squares    2.21675   7.61905   0.11491            0.89526 131.58506
#Deg. of Freedom         1         1         1                  1        53
#
#Residual standard error: 1.57567
#3 out of 8 effects not estimable
#Estimated effects may be unbalanced

请注意,结果不是很明智。我假设您的实际数据集更大。


推荐阅读