Hi, I am trying to load csv type data from a folder. However, rather than syntax that simply loads one file at a time I was wondering if there is a method that loads all data from a specific folder. For instance, I have the following data files in a folder nebraskaStats: 10-1-2009_10-7-2009.txt 10-2-2009_10-8-2009.txt 10-3-2009_10-9-2009.txt ....etc. (245 total files in folder) Each file has the following format: Data1-5,Data 2-6,Data 3-7,... 1,2,3,... Although the comma separated format is consistent file to file, the name of the columns differ in many of the files. What I want to do is simply load all that data by pointing to the folder rather than specific file names. I know this syntax: state.frame<-read.table("/nebraskaStats/", header=T, sep=",") does not seem to read data from a folder but rather it is intended to load specific files. I could write a loop that iterates the number of the files (e.g., N-S-2009_N-T-2009, whereby N,S, and T are iterated in the loop); however, this also seems cumbersome. Anyway, is there a simple method that just loads all the data in a folder rather than have to call out each specific file? Thanks in advance. This has been a great board and I appreciate all the past help. Mark [[alternative HTML version deleted]]
Something like the following might work. YOu can use 'choose.dir()' to get the directory: files <- list.files(path=yourDir) result <- do.call(rbind, lapply(files, read.csv)) On Sun, Jan 31, 2010 at 9:10 PM, Mark Altaweel <maltaweel at anl.gov> wrote:> Hi, > > I am trying to load csv type data from a folder. However, rather than > syntax that simply loads one file at a time I was wondering if there > is a method that loads all data from a specific folder. > > For instance, I have the following data files in a folder nebraskaStats: > > 10-1-2009_10-7-2009.txt > 10-2-2009_10-8-2009.txt > 10-3-2009_10-9-2009.txt > ....etc. (245 total files in folder) > > Each file has the following format: > > Data1-5,Data 2-6,Data 3-7,... > 1,2,3,... > > Although the comma separated format is consistent file to file, the > name of the columns differ in many of the files. > > > What I want to do is simply load all that data by pointing to the > folder rather than specific file names. > > I know this syntax: ? state.frame<-read.table("/nebraskaStats/", > header=T, sep=",") does not seem to read data from a folder but rather > it is intended to load specific files. I could write a loop that > iterates the number of the files (e.g., N-S-2009_N-T-2009, whereby > N,S, and T are iterated in the loop); however, this also seems > cumbersome. Anyway, is there a simple method that just loads all the > data in a folder rather than have to call out each specific file? > > Thanks in advance. This has been a great board and I appreciate all > the past help. > > Mark > > > > ? ? ? ?[[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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Augusto.Sanabria at ga.gov.au
2010-Feb-01 03:32 UTC
[R] Loading data from folder [SEC=UNCLASSIFIED]
Hi Mark, You can use: all_files <- dir(pattern="Data") One_file <- all_files[1] Is this what you want? Cheers, Augusto -------------------------------------------- Augusto Sanabria. MSc, PhD. Mathematical Modeller Risk & Impact Analysis Group Geospatial & Earth Monitoring Division Geoscience Australia (www.ga.gov.au) Cnr. Jerrabomberra Av. & Hindmarsh Dr. Symonston ACT 2601 Ph. (02) 6249-9155 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Altaweel Sent: Monday, 1 February 2010 1:11 To: r-help at r-project.org Subject: [R] Loading data from folder Hi, I am trying to load csv type data from a folder. However, rather than syntax that simply loads one file at a time I was wondering if there is a method that loads all data from a specific folder. For instance, I have the following data files in a folder nebraskaStats: 10-1-2009_10-7-2009.txt 10-2-2009_10-8-2009.txt 10-3-2009_10-9-2009.txt ....etc. (245 total files in folder) Each file has the following format: Data1-5,Data 2-6,Data 3-7,... 1,2,3,... Although the comma separated format is consistent file to file, the name of the columns differ in many of the files. What I want to do is simply load all that data by pointing to the folder rather than specific file names. I know this syntax: state.frame<-read.table("/nebraskaStats/", header=T, sep=",") does not seem to read data from a folder but rather it is intended to load specific files. I could write a loop that iterates the number of the files (e.g., N-S-2009_N-T-2009, whereby N,S, and T are iterated in the loop); however, this also seems cumbersome. Anyway, is there a simple method that just loads all the data in a folder rather than have to call out each specific file? Thanks in advance. This has been a great board and I appreciate all the past help. Mark [[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.