I need a method to divide a vector (bloodsample values) by each level of a factor of time intervals (gacat) and compare these data (by t.test/ANOVA or Kruskal Wallis) between two levels in an outcome factor variable (EPL (yes/no))
In the mtcars df:
df1 <- mtcars
df1$cyl <- factor(df1$cyl)
df1$gear <- factor(df1$gear)
This code solves my problem nicely., using anova:
lapply(split(df1, df1$gear), function(d){summary(aov(mpg~cyl, data=d))})
However, as the last level of my data in split
(in the above example gear) only has one of the yes/no outcome, the entire code throws the error:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
My data:
No Yes
<6 weeks 89 21
6-8 weeks 166 37
8-10 weeks 158 18
10-12 weeks 131 5
>12 weeks 90 **0**
It is the zero in * that seems to be the problem... In the example, this is not a problem as every factor has at least two levels:
table(df1$cyl, df1$gear)
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
As I need to examine a lot of blood samples, I would love a way to do these comparisons by a short code-block. Any way to make R throw a NaN for the last level, in stead of dropping the entire code?