I'm trying to create an lapply
function to run multiple t.test
s for multiple levels of grouping. I came across this question: Kruskal-Wallis test: create lapply function to subset data.frame? but they were only trying to group by one variable (phase
). I would like to add another grouping level color
, where my iv is distance
and dv is val
grouped by color
then phase
.
# create data
val<-runif(60, min = 0, max = 100)
distance<-floor(runif(60, min=1, max=3))
phase<-rep(c("a", "b", "c"), 20)
color<-rep(c("red", "blue","green","yellow","purple"), 12)
df<-data.frame(val, distance, phase, color)
Their answer for the grouping by phase
was
lapply(split(df, df$phase), function(d) { kruskal.test(val ~ distance, data=d) })
However, it doesn't account for another level (color
) for grouping. I might be approaching this wrong so I appreciate any help.