Re: [R] Indexing within scan

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Wed 20 Jul 2005 - 08:22:35 EST

"Justin Rhodes" <rhodesju@ohsu.edu> writes:

> Dear R-help subscribers,
>
> Can some one please help me figure out how to write code that will allow me to use a for loop to scan a number of files one by one, and then save a summary of each file as the for loop progresses. For example I have 24 files named a1 through a24, and I want to do something like:
>
>
> results<-numeric(24)
> for (i in 1:24)
> {
>
> p<-scan("ai.txt") # where the i is an index for each of the 24 files
>
> results[i]<-mean(p)
> }
>
> Thanks for any help,
>

paste("a", i, ".txt", sep="")

should get you in the right direction. However, I don't think you *really* want a for loop. Use vectorization and sapply instead:

names <- paste("a", 1:24, ".txt", sep="") results <- sapply(names, function(n) mean(scan(n)) )

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Jul 20 08:32:24 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:50 EST