I'm trying to make a loop with many files...> library(dplR) > > files <- system("ls *.rwl", intern=TRUE) > > files[1] "cimfasy.rwl" "rocquce.rwl"> for (i in files) {a <- read.rwl(i,header=0)}There are 70 series There are 21 series> class(a)[1] "data.frame" This loop import all the files rwl in a single data.frame ( a ). Can I import a single files to a single data.frame? like this: for (i in files) {cimfasy <- read.rwl(i,header=0)} for (i in files) {rocquce <- read.rwl(i,header=0)} Thanks in advance, Alfredo
Alfredo Alessandrini wrote:> I'm trying to make a loop with many files... > > > >> library(dplR) >> >> files <- system("ls *.rwl", intern=TRUE) >> >> files >> > [1] "cimfasy.rwl" "rocquce.rwl" > >> for (i in files) {a <- read.rwl(i,header=0)} >> > There are 70 series > There are 21 series > >> class(a) >> > [1] "data.frame" > > > This loop import all the files rwl in a single data.frame ( a ). > > Can I import a single files to a single data.frame? like this: > > for (i in files) {cimfasy <- read.rwl(i,header=0)} > > for (i in files) {rocquce <- read.rwl(i,header=0)} > > >not easy to get what you really want. the first loop above does read each file separately and place it into a, but at each step it replaces the content of a with a new one. so a contains only the content of the last file, and the whole loop makes no sense. the other two loops do quite the same, just that now you have additional two variables filled with the contents of the same file, the last one on your list. makes no sense either. can you *explain* what the goal is? vQ
Alfredo Alessandrini wrote:>> can you *explain* what the goal is? >> > > I want import any rwl files (cimfasy.rwl, rocquce.rwl, .......), in a > data.frame with the name like to name of file rwl: > > cimfasy.rwl -> cimfasy > > rocquce.rwl -> rocquce > > with this loop: > > >> library(dplR) >> >> files <- system("ls *.rwl", intern=TRUE) >> >> files >> > [1] "cimfasy.rwl" "rocquce.rwl" > >> for (i in files) {a <- read.rwl(i,header=0)} >>for (file in files) assign(gsub("\\.rwl$", "", file), read.rwl(file, header=0)) ?assign ?gsub vQ
Hi, I am not sure this is what you want ?. But alist <- list.files(path = "whatever path you have for your files", full.names = TRUE) Now you have a list with the names of your files ?. Form each name you can make a variable name and assign that file ?. fname <- substring(alist[i], first, last) n <- length(alist) resultList <- list() for( i in 1:n){ resultList[[i]] <- read.table(alist[i]) ### this only if the *.rwl is a kind of table ?. I am not familiar with this extension so I have no idea how you read it in R } names(resultList) <- fname Now you have a list of your data named as you want ?.. you have access to it or any of the list members I am not taking credit .... the question about using a list to name variables was put before and Greg Snow came up with the list() idea - i think. Hope this helps if this is what you want, Monica Message: 4 Date: Tue, 24 Jun 2008 12:37:44 +0200 From: Wacek Kusnierczyk Subject: Re: [R] loop with files To: R help Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Alfredo Alessandrini wrote:>> can you *explain* what the goal is? >> > > I want import any rwl files (cimfasy.rwl, rocquce.rwl, .......), in a > data.frame with the name like to name of file rwl: > > cimfasy.rwl -> cimfasy > > rocquce.rwl -> rocquce > > with this loop: > > >> library(dplR) >> >> files <- system("ls *.rwl", intern=TRUE) >> >> files >> > [1] "cimfasy.rwl" "rocquce.rwl" > >> for (i in files) {a <- read.rwl(i,header=0)} >>for (file in files) assign(gsub("\\.rwl$", "", file), read.rwl(file, header=0)) ?assign ?gsub vQ _________________________________________________________________ The i?m Talkathon starts 6/24/08.? For now, give amongst yourselves.