Question: How to save nested for loop data in R
0
gravatar for archana.bioinfo87
20 months ago by
archana.bioinfo87180 wrote:

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 • 321 views
ADD COMMENTlink modified 20 months ago • written 20 months ago by archana.bioinfo87180

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 REPLYlink written 20 months ago by ATpoint44k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1076 users visited in the last hour