1

I am having a problem with a loop. I have a data frame:

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,)

I want to subset the dataframe by Transcript and paste column E with S together to give all possible combinations in each transcript. For example, for Transcript 1, I want to return:

AAAaaa,AAAttt,AAAccc,AAAggg,TTTaaa,TTTttt,TTTccc,TTTggg,CCCaaa,CCCttt,CCCccc,CCCggg,GGGaaa,GGGttt,GGGccc,GGGggg.

I have tried the following loop, but it only returns the AAAaaa AAAttt AAAccc AAAggg:

for(i in 1:nrow(s[[1]])){p=paste(s[[1]][1,4],s[[1]][1:i,3],sep="")}

How to I create this loop?

0

2 Answers 2

2

Something like this might work:

lapply(s, function(df) apply(expand.grid(df$E, df$S),1,paste0,collapse=""))

Or, as proposed by @akrun,

lapply(s, function(df) do.call(paste0, expand.grid(df$E, df$S)))
Sign up to request clarification or add additional context in comments.

2 Comments

Good use of expand.grid!
You could also do lapply(s, function(df) do.call(paste0, expand.grid(df$E, df$S)))
1

You could try data.table

library(data.table)
setDT(DF)[,Reduce(paste0,CJ(E, S)), by=Transcript]

data

DF <- data.frame(Transcript, Exon, S, E)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.