r - 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?
解决方案
从您的问题中不清楚您的自变量是什么。在以下示例中,我假设Site
,Location
是Replicate
您的 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
请注意,结果不是很明智。我假设您的实际数据集更大。
推荐阅读
- powershell - 使用 Powershell 将新的 O365 用户添加到 AD 组
- django - 如何使用 django 更改 url 以显示描述而不是 id?
- azure-cosmosdb - 批量插入 Azure Cosmos DB
- codespaces - 在 VS Codespace 中复制一行的快捷键是什么
- python - Python 初始化失败,没有名为“编码”的模块错误
- reactjs - 使用 react dropzone 显示文件名
- perforce - perforce -m 属性为 clean 不在文档中
- amazon-dynamodb - 扫描整个发电机数据库并根据条件更新记录
- flutter - 颤振 - ImagePicker 问题
- c++ - 如何在 C++ 中找到序列中最大元素的出现次数?