[R] Loops for repetitive task
Peter Alspach
Peter.Alspach at plantandfood.co.nz
Wed Aug 10 07:01:13 CEST 2011
Tena koe
Try something along the following lines:
chrData <- vector('list', 22)
names(chrData) <- paste('chr', 1:22, sep='')
for (i in 1:length(chrData))
{
chrData[[i]] <- read.table(file=paste('chr', i, '.out.txt', sep=''), header=F)
...
}
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of a217
> Sent: Wednesday, 10 August 2011 4:32 p.m.
> To: r-help at r-project.org
> Subject: [R] Loops for repetitive task
>
> 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","stran
> d")
> 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 at 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.
The contents of this e-mail are confidential and may be subject to legal privilege.
If you are not the intended recipient you must not use, disseminate, distribute or
reproduce all or any part of this e-mail or attachments. If you have received this
e-mail in error, please notify the sender and delete all material pertaining to this
e-mail. Any opinion or views expressed in this e-mail are those of the individual
sender and may not represent those of The New Zealand Institute for Plant and
Food Research Limited.
More information about the R-help
mailing list