Hi, I would like to open several tables with a loop, using something like : ----------------- $ ls 1.txt 2.txt 3.txt 4.txt $ R> for (i in 1:4) tabi<-read.table("i.txt")Error in file(file, "r") : unable to open connection In addition: Warning message: cannot open file `i.txt' ------------------ thanks for any help Tristan Lefebure
lefebure tristan wrote:> Hi, > I would like to open several tables with a loop, using something like : > ----------------- > $ ls > 1.txt 2.txt 3.txt 4.txt > $ R > >>for (i in 1:4) tabi<-read.table("i.txt")Since i is within a character string, it cannot be used as a variable in your case. You may paste() is together: paste(i, ".txt", sep=""), or even better, read the directories contents with list.files() and proceed over the result as in: tab <- lapply(list.files(pattern="^?[[:digit:]]\.txt"), read.table) Uwe Ligges> Error in file(file, "r") : unable to open connection > In addition: Warning message: > cannot open file `i.txt' > ------------------ > > thanks for any help > > Tristan Lefebure > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Fri, 26 Dec 2003, lefebure tristan wrote:> I would like to open several tables with a loop, using something like : > ----------------- > $ ls > 1.txt 2.txt 3.txt 4.txt > $ R > > for (i in 1:4) tabi<-read.table("i.txt")Use read.table(paste(i, "txt", sep="."))> Error in file(file, "r") : unable to open connection > In addition: Warning message: > cannot open file `i.txt' > -------------------- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
And if your intention is to create 4 data frames with names tab1, tab2, tab3, tab4 then combine Prof. Riley's advice with assign like this: for(i in 1:4) assign( paste("tab", i, sep=""), read.table(paste(i, "txt", sep=".")) ) --- Date: Fri, 26 Dec 2003 14:07:45 +0000 (GMT) From: Prof Brian Ripley <ripley at stats.ox.ac.uk> To: lefebure tristan <lefebure at univ-lyon1.fr> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] loop and read.table On Fri, 26 Dec 2003, lefebure tristan wrote:> I would like to open several tables with a loop, using something like : > ----------------- > $ ls > 1.txt 2.txt 3.txt 4.txt > $ R > > for (i in 1:4) tabi<-read.table("i.txt")Use read.table(paste(i, "txt", sep="."))> Error in file(file, "r") : unable to open connection > In addition: Warning message: > cannot open file `i.txt' > ------------------