[R] Problem with loop

R. Michael Weylandt michael.weylandt at gmail.com
Fri Dec 2 18:55:31 CET 2011


You never create a variable called "Mat2002273" or "Mat2002361" so you
can't ask R to loop over all the values between them.

If I were you, I'd code something like this:

lf <- list.files()

# PUT IN SOME CODE TO REMOVE FILES YOU DON'T WANT TO USE

pv <- vector("numeric", length(lf))

for(i in lf) pv[i] <- mean( read.csv(lf, header = TRUE)[,"Pixelvalues"])

print(pv)

Michael

On Fri, Dec 2, 2011 at 12:15 PM, Komine <momadou at yahoo.fr> wrote:
> Hi,
> I try to build a loop difficultly.
> I have in a folder called Matrices several files (.csv) called Mat2002273,
> Mat2002274  to Mat2002361.
> I want to calculate for each file the mean of the column called Pixelvalues.
> I try this code but as result, I have this message:  Mat2002273 not found
>
>>essai<-read.table("C:\\Users\\Desktop\\Matrices\\Mat2002273.csv",sep=";",dec=",",header=TRUE)
>>essai
>>a <- NULL
>>for(i in Mat2002273:Mat2002361){
>>paste(mean(essai$Pixelvalues))
>>a[i] <- paste(mean(essai$Pixelvalues))
>>print(a[i])
>>}
>
> Thank you for your help
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-with-loop-tp4148083p4148083.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.



More information about the R-help mailing list