Re: [R] How to read more than 1 table?

From: Kjetil Brinchmann Halvorsen <kjetilbrinchmannhalvorsen_at_gmail.com>
Date: Tue 21 Feb 2006 - 09:50:19 EST

Atte Tenkanen wrote:
> Question 1) I want to read many csv-tables and run the same commands for
> all of them. Is there some simpler solution than this below to solve this
> problem?
>
>
> for (g in 1:6)
> {
>
> if (g==1){k="kt1_0057"}
> if (g==2){k="kt1_0101"}
> if (g==3){k="kt1_0613"}
> if (g==4){k="staten"}
> if (g==5){k="tenpenny"}
> if (g==6){k="fiddrunk"}
>
> TABLE=read.table(paste("/home/user/",k,".csv",sep=""),sep = ",",
> na.strings=".",header=F,fill=TRUE);

put your filenames in a character vector filenames: filenames <- paste( c("kt1_0057", ...), ".csv", sep="") tables <- lapply( filenames, function(x) read.table(file=x, na.strings= ..., ...) )

and if you want to repeat the same task for your six tables do it with lapply() or sapply()

>
> print(TABLE)
>
> }
>
> Question 2) Is it possible to create new variables for example with the
> assistance of for-loop without initialising them beforehand?

This way:

    sapply(1:10, function(i) your.task(i) )

sapply() will do the initialization for you!

Kjetil

>
> ______________________________________________
> 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
>



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 Tue Feb 21 10:20:37 2006

This archive was generated by hypermail 2.1.8 : Tue 21 Feb 2006 - 14:38:23 EST