Hello,

I have an R script that I use as a template to perform a task for multiple
files (in this case, multiple chromosomes).

What I would like to do is to utilize a simple loop to parse through each
chromosome number so that I don't have to type the same code over and over
again in the R console.

I've tried using:

for(i in 1:22){
etc..
}

and replacing each chromosome number with [[i]], but that did not seem to
work.

Below is the script I have. Basically everywhere you see a '2' I would like
there to be an 'i' so that the script can be applied in a general sense.
################################Code###############################

chr2.data<-read.table(file="chr2.out.txt", header=F)
colnames(chr2.data)<-c("chr","start","end","base1","base2","totalreads","methylation","strand")
splc2<-split(chr2.data, paste(chr2.data$chr))
chr2.df<-as.data.frame(t(sapply(splc2, function(x)
list(TR=NROW(x[['totalreads']]),    RG1=sum(x[['totalreads']]>=1),
percent=(NROW(x[['totalreads']]>=1)/sum(x[['totalreads']]))))))
chr2.df.summ<-as.data.frame(t(sapply(splc2, function(x)
summary(x$methylation))))
chr2.summ<-cbind(chr2.df,chr2.df.summ)

##################################################################


Here are some sample input files in case you'd like to test the code:
##########
# chr1.out.txt
##########
chr1    100     159     104     104     1       0.05    +
chr1    100     159     145     145     1       0.04    +
chr1    200     260     205     205     1       0.12    +
chr1    500     750     600     600     1       0.09    +

##########
# chr2.out.txt
##########
chr2    100     200     105     105     1       0.03    +
chr2    100     200     110     110     1       0.08    +
chr2    300     400     350     350     0       0       +


The code works perfectly fine just typing everything out by hand, but that
is very inefficient given that there are 24 chromosomes for each dataset. I
am just looking for any suggestions as to how I can write a general version
of this code.


--
View this message in context: 
http://r.789695.n4.nabble.com/Loops-for-repetitive-task-tp3732022p3732022.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to