How to save nested for loop data in R
0
0
Entering edit mode
5.0 years ago

Hi, I am trying to extract few sequnces from a csv file. I am unable to save the result. The for loop is working once.

fasta <- read.csv("trial_seq.csv", header = TRUE, sep =",")
fasta
a<- as.character(fasta$Seq)
a   ###original file 
b = matrix(nrow=6, ncol=10)

for (i in 1:length(a)){
  for ( j in 1:nchar(a[i])) ######Succesfully extract 2-6 nt from start postion
  {
    b[j] <- substr(a, (j-(j-1)), j+1)
    ifelse ((j<=5),{print(b[j])}, break())
  }
  }
b
#####################File stored in "a"
[1] "ACGTATTGATGCCACAGACGTATTGATGCCACAGACGTATTGATGCCACAG"    
[2] "ACGTATTGATGCCACAGACGTATTGATGCCACAGACGTATTGATGCCACCC"    
[3] "ACGTATTGATGCCACAGACGTATTGATGCCACAGACGTATTGATGCCACTT"

Any help is much appreciated.

Thanks

R • 816 views
ADD COMMENT
0
Entering edit mode

Can you give a representative example of the input (ok you did that already) and the expected output. I am 99.99% sure this can be done with a simply one-liner avoiding any loop and if statements.

ADD REPLY

Login before adding your answer.

Traffic: 1406 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6