[R] loop with files
Monica Pisica
pisicandru at hotmail.com
Wed Jun 25 16:41:56 CEST 2008
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.
More information about the R-help
mailing list