每列均采用一种方差分析 [英] One way ANOVA for every column
问题描述
我需要在基于另一列的数据框中的每一列上运行单向方差分析.我的数据框有57列,因此键入每个列名非常耗时.这是我的数据框的一部分.
I need to run a one way ANOVA on every column in a data frame based on another column. My data frame has 57 columns, so it is very time consuming to type out every column name. Here is part of my data frame.
所以基本上,我需要为每列运行此函数
So basically, I need this function run for every column
aov(df$PGY_16 ~ df$Total_Time_cm_16, df)
因此,我需要一个循环来为数据帧中的每一列运行该循环.
So I need a loop to run that for every column in my data frame.
任何帮助将不胜感激!
推荐答案
为了可重复性,下面的代码使用内置的mtcars
数据框并返回一个列表,其中每个列表元素都是aov
模型的mtcars$cyl
与数据框中的所有其他列.我们使用purrr
软件包(属于tidyverse
软件包套件的一部分)中的map
来照顾在数据帧的每一列上连续运行aov
.
For reproducibility, the code below uses the built-in mtcars
data frame and returns a list in which each list element is an aov
model of mtcars$cyl
with every other column in the data frame. We use map
from the purrr
package (which is part of the tidyverse
suite of packages) to take care of successively running aov
on each column of the data frame.
library(tidyverse)
aov.models = mtcars[ , -grep("cyl", names(mtcars))] %>%
map(~ aov(mtcars$cyl ~ .x))
对于您的数据,类似的代码为:
For your data, the analogous code would be:
aov.models = df[ , -grep("PGY_16", names(df))] %>%
map(~ aov(df$PGY_16 ~ .x))
这篇关于每列均采用一种方差分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!