【发布时间】:2014-10-03 03:10:29
【问题描述】:
我有两个数据框/数据列表“humanSplitandratSplit”,它们的格式为
> ratSplit$Kidney_F_GSM1328570
ratGene ratReplicate alignment RNAtype
1 Crot Kidney_F_GSM1328570 7 REV
2 Crot Kidney_F_GSM1328570 12 REV
3 Crot Kidney_F_GSM1328570 4 REV
和
> humanSplit$Fetal_Brain_408_AGTCAA_L009_R1_report.txt
humanGene humanReplicate alignment RNAtype
53 ZFP28 Fetal_Brain_408_AGTCAA_L009_R1_report.txt 5 reg
55 RC3H1 Fetal_Brain_408_AGTCAA_L009_R1_report.txt 9 reg
56 IFI27 Fetal_Brain_408_AGTCAA_L009_R1_report.txt 4 reg
下面使用的另一个文件是geneList的形式:
ABAT,Abat
ABCA1,Abca1
ABCA12,Abca12
ABCA2,Abca2
ABCA3,Abca17
ABCA4,Abca4
ABCA5,Abca5
现在我想在经过一些数据处理后在ratSplit 和humanSplit 之间的所有元素对组合之间进行费希尔精确测试。并最终想将 Fisher 测试的结果写入csv 文件中。现在我正在做双重循环。但我想知道如何使用sapply 或其他相关的东西来提高效率。
目前我正在做以下事情:在这里我首先制作一个data.frameresult,在其中我存储/附加从费舍尔测试中获得的所有信息在每个步骤中成对。最后,当整个循环完成时,我将最终的result 写入csv 文件。我的理解是使用sapply我需要将循环内部转换成一个函数,然后调用sapply。但我不确定优化它的最佳方法是什么。任何帮助将不胜感激
result <- data.frame(humanReplicate = "human_replicate", ratReplicate = "rat_replicate", pvalue = "p-value", alternative = "alternative_hypothesis",
Conf.int1 = "conf.int1", Conf.int2 ="conf.int2", oddratio = "Odd_Ratio")
for(i in 1:length(ratSplit)) {
for(j in 1:length(humanSplit)) {
ratReplicateName <- names(ratSplit[i])
humanReplicateName <- names(humanSplit[j])
#merging above two based on the one-to-one gene mapping as in geneList defined above.
mergedHumanData <-merge(geneList,humanSplit[[j]], by.x = "human", by.y = "humanGene")
mergedRatData <- merge(geneList, ratSplit[[i]], by.x = "rat", by.y = "ratGene")
mergedHumanData <- mergedHumanData[,c(1,2,4,5)] #rearrange column
mergedRatData <- mergedRatData[,c(2,1,4,5)] #rearrange column
mergedHumanRatData <- rbind(mergedHumanData,mergedRatData) #now the columns are "human", "rat", "alignment", "RNAtype"
agg <- aggregate(RNAtype ~ human+rat, data= mergedHumanRatData, FUN=getGeneType) #agg to make HmYn form
HmRnTable <- table(agg$RNAtype) #table of HmRn ie RNAtype in human and rat.
#now assign these numbers to variables HmYn. Consider cases when some form of HmRy is not present in the table. That's why
#is.integer0 function is used
HyRy <- ifelse(is.integer0(HmRnTable[names(HmRnTable) == "HyRy"]), 0, HmRnTable[names(HmRnTable) == "HyRy"][[1]])
HnRn <- ifelse(is.integer0(HmRnTable[names(HmRnTable) == "HnRn"]), 0, HmRnTable[names(HmRnTable) == "HnRn"][[1]])
HyRn <- ifelse(is.integer0(HmRnTable[names(HmRnTable) == "HyRn"]), 0, HmRnTable[names(HmRnTable) == "HyRn"][[1]])
HnRy <- ifelse(is.integer0(HmRnTable[names(HmRnTable) == "HnRy"]), 0, HmRnTable[names(HmRnTable) == "HnRy"][[1]])
contingencyTable <- matrix(c(HnRn,HnRy,HyRn,HyRy), nrow = 2)
fisherTest <- fisher.test(contingencyTable)
newLine <- data.frame(t(c(humanReplicate = humanReplicateName, ratReplicate = ratReplicateName, pvalue = fisherTest$p,
alternative = fisherTest$alternative, Conf.int1 = fisherTest$conf.int[1], Conf.int2 =fisherTest$conf.int[2],
oddratio = fisherTest$estimate[[1]])))
result <-rbind(result,newLine)
}
}
write.table(result, file = "newData5.csv", row.names = FALSE, append = FALSE, col.names = TRUE, sep = ",")
【问题讨论】:
-
看看
outer。看起来你可以使用它
标签: r list for-loop apply sapply