An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130102/64bd7e25/attachment-0001.pl>
Read many cvs files
4 messages · Dominic Roye, Yuan, Rebecca, jim holtman
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.
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}}
use the XLConnect package to read/write Excel workbooks. It handles multiple sheets just fine. On Wed, Jan 2, 2013 at 12:03 PM, Yuan, Rebecca
<rebecca.yuan at bankofamerica.com> wrote:
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...{{dropped:15}}