This is what I do (a lot): If all the files you want to read have the same structure, I would suggest using dlply in package plyr to import them.
First set you working directory to the location of where you have your files ( use setwd()), then construct a vector of filenames you want to bring in: library(plyr) myfiles <- c("file1.csv", "file2.csv") mydata <- dlply(myfiles, .progress="text") HTH James James T. Durant, MSPH CIH Environmental Health Scientist US Agency for Toxic Substances and Disease Registry Atlanta, GA 30341 770-488-0668 Message: 21 Date: Wed, 2 Jan 2013 16:14:57 +0100 From: Dominic Roye <dominic.r...@gmail.com> To: R help <r-help@r-project.org> Subject: [R] Read many cvs files Message-ID: <calvvs-ezhimujh3ajntmaep60dvvnuna7ltjdq0n6yidmz2...@mail.gmail.com> Content-Type: text/plain 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@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.