Combine for loops
I have two questions about my code. First I will show you the code:
partialname5 <- "EV"
EV <- ttest[grep(partialname5, rownames(ttest)),]
partialname1 <- "MYC"
MYC <- ttest[grep(partialname1, rownames(ttest)),]
MYCEV<-list()
for (j in 1:ncol(MYC)) {
t1<-t.test(MYC[,j], (EV[,j]))$p.value
MYCEV[[j]]<- matrix(t1)
}
partialname2 <- "OBX"
OBX <- ttest[grep(partialname2, rownames(ttest)),]
OBXEV<-list()
for (k in 1:ncol(OBX)) {
t2<-t.test(OBX[,k], (EV[,k]))$p.value
OBXEV[[k]]<- matrix(t2)
}
partialname3 <- "WDR"
WDR <- ttest[grep(partialname3, rownames(ttest)),]
WDREV<-list()
for (l in 1:ncol(WDR)) {
t3<-t.test(WDR[,l], (EV[,l]))$p.value
WDREV[[l]]<- matrix(t3)
}
partialname开发者_StackOverflow中文版4 <- "PIM"
PIM <- ttest[grep(partialname4, rownames(ttest)),]
PIMEV<-list()
for (m in 1:ncol(PIM)) {
t4<-t.test(PIM[,m], (EV[,m]))$p.value
PIMEV[[m]]<- matrix(t4)
}
My first question is, is it possible to fill in my partialnames by this way:
partialnames<- c('EV','PIM','OBX','MYC','WDR')
And then by the first loop take the first argument.
the second question is it possible to combine this loops for all partialnames
?
Thank you!
Samantha
A reproducible example would help, but the short answer is yes. I think you're trying to t.ttest all combinations of values for identifying variables (rownames) of EV, PIM, OBX, MYC, and WDR, for each column. Is that right?
If so, this might work, but it's hard to tell without a reproducible example.
splits <- c('EV', 'PIM', 'OBX', 'MYC', 'WDR')
results <- list()
for(i in 1:length(splits) {
for(j in 1:length(splits) {
for(k in 1:ncol(ttest)) {
results[i][j][k] <- t.test(ttest[rownames(ttest)==splits[i],k], ttest[rownames(ttest)==splits[j],k])
}
}
}
精彩评论