我遇到循环问题。 我有一个数据框:
Transcript=c(1,1,1,1,2,2,2,2,2)
Exon=rep(c(1:4,1:5))
S=c("aaa","ttt","ccc","ggg","ata","tat","cgc","gcg","bbb")
E=c("AAA","TTT","CCC","GGG","ATA","TAT","CGC","GCG","BBB")
DF=data.frame(Transcript, Exon, S, E)
DF
s=split( DF , DF$Transcript,)
我希望通过Transcript对数据框进行子集化,并将E列与S一起粘贴,以在每个记录中提供所有可能的组合。例如,对于Transcript 1,我想返回:
AAAAAA,AAAttt,AAAccc,AAAggg,TTTaaa,TTTTTT,TTTccc,TTTggg,CCCaaa,CCCttt,CCCCCC,CCCGGG,GGGaaa,GGGttt,GGGCCC,GGGGGG。
我尝试了以下循环,但它只返回AAAaaa
AAAttt
AAAccc
AAAggg
:
for(i in 1:nrow(s[[1]])){p=paste(s[[1]][1,4],s[[1]][1:i,3],sep="")}
如何创建此循环?
答案 0 :(得分:2)
这样的事可能有用:
lapply(s, function(df) apply(expand.grid(df$E, df$S),1,paste0,collapse=""))
或者,正如@akrun所提议的那样,
lapply(s, function(df) do.call(paste0, expand.grid(df$E, df$S)))
答案 1 :(得分:1)
您可以尝试data.table
library(data.table)
setDT(DF)[,Reduce(paste0,CJ(E, S)), by=Transcript]
DF <- data.frame(Transcript, Exon, S, E)