[R] Reading multiple files

Silvano Cesar da Costa silvano at uel.br
Thu Sep 27 00:19:51 CEST 2012


Actually the problem occurred in the command:

names = substr (filenames, 1, 7)

As the file's names are:

Dados1.raw, Dados2.raw, ..., Dados11.raw, Dados12.raw, ..., Data 35.raw

the program doesn't work. A quick way to make it work is rename the files to:

Dados01.raw, Dados02.raw, ..., Dados11.raw, Dados12.raw, ..., Data 35.raw


I do not know how to work any other way.

Thank you for the suggestions.



> If your previously posted code worked with files all having the same
> number of columns, and if your data is read in correctly without the
> colClasses argument, then I think the following code should work.
>
> for(i in names){
>         filepath = file.path("~/Silvano/Dados", paste(i, ".raw", sep=""))
>         assign(i, read.table(filepath))
>         }
>
> If not, then my guess is the error (what is the error message???) is due
> to the file referencing or naming.  For example, it's not clear to me why
> you take the substring of the file name, only to paste on the file type
> suffix later.
>
> Jean
>
>
>
> "Silvano Cesar da Costa" <silvano at uel.br> wrote on 09/26/2012 04:31:48 PM:
>>
>> Hi,
>>
>> I didn't notice problems with this.
>>
>>
>> > Will your data be read in correctly if you do away with the colClasses
>> > argument to read.delim (or read.table)?
>> >
>> > Jean
>> >
>> >
>> >
>> > "Silvano Cesar da Costa" <silvano at uel.br> wrote on 09/26/2012 09:11:33
> AM:
>> >>
>> >> Hi,
>> >>
>> >> I have 35 data files for reading. I would like get a program for
>> >> performing reading of 35 files at once.
>> >> All are of the type: Dados1.raw, Dados2.raw and so on.
>> >>
>> >> If the files have the same number of columns, I can read with the
>> >> following commands:
>> >>
>> >> rm(list=ls())
>> >> filenames = list.files(path="~/Silvano/Arq", pattern="Dados+.*raw")
>> >> names = substr(filenames, 1, 7)
>> >>
>> >> for(i in names){
>> >>   filepath = file.path("~/Silvano/Dados", paste(i, ".raw", sep=""))
>> >>   assign(i, read.delim(filepath,
>> >>                        colClasses=c(rep("character", 5),
> rep("numeric",
>> > 5)),
>> >>                        sep = ""))
>> >> }
>> >>
>> >> It happens that the files have different number of columns. And I
> can't
>> >> solve the problem.
>> >>
>> >> Any suggestions?
>> >>
>> >>
>> >> ---------------------------------------------
>> >> Silvano Cesar da Costa
>> >>
>> >> Universidade Estadual de Londrina
>> >> Centro de Ciências Exatas
>> >> Departamento de Estatística
>> >>
>> >> Fone: (43) 3371-4346
>


---------------------------------------------
Silvano Cesar da Costa

Universidade Estadual de Londrina
Centro de Ciências Exatas
Departamento de Estatística

Fone: (43) 3371-4346




More information about the R-help mailing list