how to compare x-group means in y-groups separately (apply in apply)

Hello,
here i have a dataframe with 5 variables (2 categorical and 3 numerical). Categorical variables are both factors (1-patient groups: "prmdiag", 2-awareness groups: "aware3"), numerical variables represent 2 clinical biomarkers ("Aß42" and "ptau") and 1 awareness score("aw_tot"). i want to perform ANOVA in each patient group (HC, SCD, MCI, AD) separately, to check if the mean of clinical biomarkers are significantly differ from each other between awareness groups (-1, 0 , 1).

library(stats)

here is the small version of the data frame:

df_ex <- data.frame(prmdiag = as.factor(c("AD", "SCD"," AD", "SCD", "HC", "MCI", "AD", "SCD", "HC", "SCD", "MCI", "MCI", "MCI", "HC", "SCD", "HC", "SCD", "MCI", "MCI", "HC")),
aw_tot = c(-0.25, 0.25, 0, -0.25, 0.25, -0.25, -0.25, 0, -0.25, 0.5, 0, -0.25, -0.25, 0, 0.25, 0.25, -0.25, -0.25, 0.25, 0.25),
Aß42 = c(293, 560, 1115, 694, 801, 586, 397, 323, 702, 981, 543, 276, 481, 961, 280, 451, 603, 440, 531, 1077),
ptau = c(84.587, 27.7, 44.604, 41.842, 40.2, 185, 125.53, 59.727, 32.5, 36.835, 102.9, 16.852, 89.235, 47.033, 23.6, 22, 148, 28.898, 88.914, 51.504),
aware3 = as.factor(c("-1", "1", "0", "-1", "1", "-1", "-1", "0", "-1", "1", "0", "-1", "-1", "0", "1", "1", "-1", "-1", "1", "1"))
)

with this code i could compare the mean of clinical biomarkers between the awareness groups (aware3) but without any separation according to patient groups(prmdiag):

formulae_ex<-lapply(colnames(df_ex)[c(3,4)], function(x) as.formula(paste0(x, " ~ aware3")))
ANOVA_ex <- lapply(formulae_ex, function(x) summary(aov(x, data=df_ex)))
names(ANOVA_ex) <- format(formulae_ex)
ANOVA_ex

how would it be possible to perform ANOVA between awareness groups, in the patient groups separately? where and how to put the variable 'prmdiag'?

i would be delighted for any help or improvement of the code. thanks a lot!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.