[R] Read many cvs files

Yuan, Rebecca rebecca.yuan at bankofamerica.com
Wed Jan 2 18:03:23 CET 2013


I know that R can read cvs file, but could R read excel files?

For example, in the workbook, I have 3 sheets. How could I read for those three sheets?

Thanks,

Rebecca
 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jim holtman
Sent: Wednesday, January 02, 2013 11:58 AM
To: Dominic Roye
Cc: R help
Subject: Re: [R] Read many cvs files

You can read the files into a "list" -- you don't want to create unique object names for each one.


files <- list.files(..pattern to match files..) results <- lapply(files, read.table, header = TRUE)


On Wed, Jan 2, 2013 at 10:14 AM, Dominic Roye <dominic.roye at gmail.com> wrote:
> Hello R helpers,
>
> I would like to automate this code for many files of the same type. 
> But I don´t know how to make it. In particular, i don´t know how to 
> read many files each one as an r object with the name of the file. 
> Then a for loop would be sufficient, right?
>
> Many thanks and a happy new year.
>
> Dominic
>
> datos <- 
> read.table('global2001.csv',head=T,sep=';',stringsAsFactors=F)
>
> datos[datos==""] <- NA
>
> temp <- datos[,19:53]
>
>
> y <- split(temp,1:nrow(temp))
> y2 <- lapply(y,function(x) paste(x[!is.na(x)],collapse=";"))
>
> global2001 <- datos[,-19:-53]
>
> global2001[1,"CODPA"] <- 1
> global2001$CODPA <- y2
> global2001$CODPA <- unlist(global2001$CODPA)
>
>
>
>
>
> Example of a file:
>
>> str(datos)
> 'data.frame':   112598 obs. of  53 variables:
>  $ Identificación.do.Paciente       : chr  "34" "41" "42" "46" ...
>  $ Número.de.Historia               : int  3024659 3028931 3030905 3033514
> 3037754 3039062 3039851 3048020 3049659 3049659 ...
>  $ TSI                              : chr  "580208NCEE0010"
> "770802ROOR0019" "710720GEUS1015" "770319GLOO0011" ...
>  $ Identificador.interno.de.episodio: int  2001033879 2001042319 
> 2001036188
> 2001030007 2001039108 2001041196 2001063540 2001031533 2001031565
> 2001056967 ...
>  $ Ano.Alta                         : int  2001 2001 2001 2001 2001 2001
> 2001 2001 2001 2001 ...
>  $ Data.Ingreso                     : chr  "16/02/2001" "27/04/2001"
> "09/03/2001" "18/01/2001" ...
>  $ Data.Alta                        : chr  "20/02/2001" "30/04/2001"
> "10/03/2001" "21/01/2001" ...
>  $ Sexo                             : chr  "    MULLER" "    MULLER" "
> HOME" "    MULLER" ...
>  $ Idade.en.anos                    : int  43 23 29 23 48 27 80 42 42 42 ...
>  $ Código.Concello                  : int  15030 15092 15030 15030 15019
> 15030 15032 15014 15005 15005 ...
>  $ Concello                         : chr  "CORUÑA, A" "VIMIANZO" "CORUÑA,
> A" "CORUÑA, A" ...
>  $ Hospital.abrev.                  : chr  "A CORUÑA" "A CORUÑA" "A CORUÑA"
> "A CORUÑA" ...
>  $ Motivo.de.Alta                   : chr  "DESTINO A DOMICILIO" "DESTINO A
> DOMICILIO" "DESTINO A DOMICILIO" "DESTINO A DOMICILIO" ...
>  $ Cód..Diagnóstico.Ppal.completo   : chr  "218.0 " "890.1 " "998.89" "650.
>  " ...
>  $ Diagnóstico.Ppal.completo        : chr  "LEIOMIOMA UTERINO SUBMUCOSO"
> "HERIDA ABIERTA DE CADERA Y MUSLO-COMPLICADA" "OTRAS COMPLICACIONES 
> ESPECIFICADAS DE PROCEDIMIENTO" "PARTO NORMAL" ...
>  $ GRD                              : int  359 443 453 373 796 372 14 758
> 450 450 ...
>  $ Descrición..do.GRD               : chr  "PROC. SOBRE ÚTERO E ANEXOS POR
> CA.IN SITU E PROCESO NON MALIGNO SEN CC" "OUTROS PROCEDEMENTOS 
> CIRÚRXICOS POR LESIÓN TRAUMÁTICA SEN CC" "COMPLICACIÓNS DE TRATAMENTO 
> SEN CC" "PARTO SEN COMPLICACIÓNS" ...
>  $ Peso.GRD                         : num  1.098 0.948 0.601 0.58 3.846 ...
>  $ Lista.de.códigos.de.diagnóstico  : chr  "218.0" "890.1" "998.89" "650."
> ...
>  $ X                                : chr  "" "E986" "780.2" "V27.0" ...
>  $ X.1                              : chr  "" "" "780.4" "" ...
>  $ X.2                              : chr  "" "" "381.3" "" ...
>  $ X.3                              : chr  "" "" "" "" ...
>  $ X.4                              : chr  "" "" "" "" ...
>  $ X.5                              : chr  "" "" "" "" ...
>  $ X.6                              : chr  "" "" "" "" ...
>  $ X.7                              : chr  "" "" "" "" ...
>  $ X.8                              : chr  "" "" "" "" ...
>  $ X.9                              : chr  "" "" "" "" ...
>  $ X.10                             : chr  "" "" "" "" ...
>  $ X.11                             : chr  "" "" "" "" ...
>  $ X.12                             : chr  "" "" "" "" ...
>  $ X.13                             : chr  "" "" "" "" ...
>  $ X.14                             : chr  "" "" "" "" ...
>  $ X.15                             : chr  "" "" "" "" ...
>  $ X.16                             : chr  "" "" "" "" ...
>  $ X.17                             : chr  "" "" "" "" ...
>  $ X.18                             : chr  "" "" "" "" ...
>  $ X.19                             : chr  "" "" "" "" ...
>  $ X.20                             : chr  "" "" "" "" ...
>  $ X.21                             : chr  "" "" "" "" ...
>  $ X.22                             : chr  "" "" "" "" ...
>  $ X.23                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.24                             : chr  "" "" "" "" ...
>  $ X.25                             : chr  "" "" "" "" ...
>  $ X.26                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.27                             : chr  "" "" "" "" ...
>  $ X.28                             : chr  "" "" "" "" ...
>  $ X.29                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.30                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.31                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.32                             : num  NA NA NA NA NA NA NA NA NA NA ...
>  $ X.33                             : logi  NA NA NA NA NA NA ...
>>
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

----------------------------------------------------------------------
This message, and any attachments, is for the intended r...{{dropped:6}}




More information about the R-help mailing list