user3130644
user3130644

Reputation: 101

simple loop in r?

I only can write some simple R code. For example:

data <- subset(PCB,PCB1.cat3 == "Low" | PCB1.cat3 == "High")
data <- data[order(data$PCB1.cat3),] ;table(data$PCB1.cat3)
mydata <- data.frame(data[,c(19:134)]);mydata <- t(mydata)
library(limma)
design <- cbind(Grp1=1,Grp2vs1=rep(c(0,1), times = c(27,26)))
fit <- lmFit(mydata,design)
fit <- eBayes(fit)
results <- topTable(fit,adjust = "fdr",coef=2, sort.by="P", number=100)

It works well for variable PCB1.cat3. However, I have 11 variables: PCB2.cat3, PCB3.cat3 ... How to make a loop and the summary of the results?

Upvotes: 0

Views: 76

Answers (1)

user3130644
user3130644

Reputation: 101

I figured out by myself. Hope it is helpful for someone who is interested.

    library(limma)
    for (i in 1:8)  
    {
      data <- PCB[,c(135:142,3:10,19:134)]
      data <- subset(data,data[,i] == "Low" | data[,i] == "High")
      data <- data[order(data[,i]),] ;table(data[,i])
      mydata <- data.frame(data[,c(17:132)]);mydata <- t(mydata)
      design <- cbind(Grp1=1,Grp2vs1=rep(c(0,1), times = c(27,26)))
      fit <- lmFit(mydata,design)
      fit <- eBayes(fit)
      results <- topTable(fit,adjust = "fdr",coef=2, sort.by="P", number=20)
      assign(paste("res.quartiles",colnames(data[i]),sep="."),results)
    }

Upvotes: 1

Related Questions