Home > Software engineering >  Convert for loop to apply
Convert for loop to apply

Time:12-12

I am trying to run an ANOVA test on a gene expression data.

enter image description here

This is what my data frame looks like. The rows are single cells while the columns are genes. The values are the gene expression values. The first column is the BMI group that each cell corresponds to. What I am trying to do is essentially find differentially expressed genes between the 3 different BMI groups.

I wrote out a for loop but because I have 28519 columns I want to use an apply function.

for (i in 2:ncol(a.data)) {
    formula <- paste(colnames(a.data)[i], baseformula, sep="")
    p <- summary(aov(as.formula(formula), data=a.data))[[1]][["Pr(>F)"]][1]
    ANOVA.table <- c(ANOVA.table,p)
}

I was wondering if I could covert this to an apply function.

CodePudding user response:

Assuming that your code works, you're code would look something like this:

index <- 2:ncol(a.data)
ANOVA.table <- c()

sapply(index, function(i) {
     formula <- paste(colnames(a.data)[i], baseformula, sep="")
     p <- summary(aov(as.formula(formula), data=a.data))[[1]][["Pr(>F)"]][1]
     ANOVA.table <- c(ANOVA.table,p)


})

Disclaimer: I did not test this code and I just wrote this based on how the code was presented.

  • Related