【发布时间】:2016-09-23 15:36:35
【问题描述】:
我正在单个数据框中跨多个组运行简单的单向方差分析。
此处提供数据框:https://www.dropbox.com/s/6nsjk4l1pgiwal3/cut1.csv?dl=0
>download.file('https://www.dropbox.com/s/6nsjk4l1pgiwal3/cut1.csv?raw=1', destfile = "cut1.csv", method = "auto")
> data <- read.csv("cut1.csv")
> cut1 <- data %>% mutate(Plot = as.factor(Plot), Block = as.factor(Block), Cut = as.factor(Cut))
> str(cut1)
'data.frame': 160 obs. of 6 variables:
$ Plot : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Block : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 2 2 2 2 3 3 ...
$ Treatment : Factor w/ 4 levels "AN","C","IU",..: 4 2 3 1 1 3 4 2 3 1 ...
$ Cut : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
$ Measurement: Factor w/ 10 levels "ADF","Ash","Crude_Protein",..: 5 5 5 5 5 5 5 5 5 5 ...
$ Value : num 956 965 961 963 955 ...
我使用了this SO question 中的一些代码来启用aov 函数以应用于Measurementfactor 的每个级别:
anova_1<- sapply(unique(as.character(cut1$Measurement)),
function(meas)aov(Value~Treatment+Block,cut1,subset=(Measurement==meas)),
simplify=FALSE,USE.NAMES=TRUE)
summary_1 <- lapply(anova_1, summary)
我可以手动查看summary_1,但理想情况下我想做的是将Measurement因子的每个级别的p值提取到一个数据框中,然后我可以过滤它,以便我只看到哪些是TukeyHSD。
summary_1 看起来像这样(仅显示前 2 个列表):
> str(summary_1)
List of 10
$ Dry_matter :List of 1
..$ :Classes ‘anova’ and 'data.frame': 3 obs. of 5 variables:
.. ..$ Df : num [1:3] 3 3 9
.. ..$ Sum Sq : num [1:3] 359 167 612
.. ..$ Mean Sq: num [1:3] 119.8 55.5 68
.. ..$ F value: num [1:3] 1.761 0.816 NA
.. ..$ Pr(>F) : num [1:3] 0.224 0.517 NA
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
$ Crude_Protein:List of 1
..$ :Classes ‘anova’ and 'data.frame': 3 obs. of 5 variables:
.. ..$ Df : num [1:3] 3 3 9
.. ..$ Sum Sq : num [1:3] 306 721 1606
.. ..$ Mean Sq: num [1:3] 102 240 178
.. ..$ F value: num [1:3] 0.572 1.347 NA
.. ..$ Pr(>F) : num [1:3] 0.647 0.319 NA
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
我可以像这样从summary_1 的列表之一中提取 p 值:
> summary_1$OAH[[1]][,5][1]
[1] 0.4734992
但是,我不知道如何从所有嵌套列表中提取并放入数据框中。
非常感谢任何帮助。
【问题讨论】:
-
另外,你试过
unsplit或data.table::rbindlist吗? -
@caffeine 我以前在这里使用 Dropbox 数据集从来没有遇到过问题
-
@C8H10N4O2 using rbindlist 给了我以下错误
Error in FUN(X[[i]], ...) : Invalid column: it has dimensions. Can't format it. If it's the result of data.table(table()), use as.data.table(table()) instead.
标签: r list nested-lists sapply anova