Dear R users
I am struggling from a data importing issue:
I have some hundreds of csv files needed to be read into R for futher
analysis. All those csv files are named in one of the three formats:
(1) strings: e.g. London_Oxford street
(2) Integer: e.g. 1234_5678
(3) combined: e.g. London_1234
I intend to use read.csv("xxxx_xxx.csv") but I only dealt with
sigle documents before and if there are only no more than 20 files, I do
not bother to search a more efficient way.
Is there any claver way that I do not have to type in all these hundreds
names by hand, maybe using a R package or write some code in some other
languages if it is not too difficult to learn.
Any thoughts/hints please??
Many thanks in advance!
HJ
[[alternative HTML version deleted]]
See ?dir Assign the value to a vector and loop over the elements of the vector. Kevin On Fri, May 25, 2012 at 12:16 PM, HJ YAN <yhj204 at googlemail.com> wrote:> Dear R users > > > I am struggling from a data importing issue: > > I have some hundreds of csv files needed to be read into R for futher > analysis. All those csv files are named in one of the three formats: > > (1) strings: e.g. London_Oxford street > (2) Integer: e.g. 1234_5678 > (3) combined: e.g. London_1234 > > I intend to use read.csv("xxxx_xxx.csv") but I only dealt with > sigle documents before and if there are only no more than 20 files, I do > not bother to search a more efficient way. > > > Is there any claver way that I do not have to type in all these hundreds > names by hand, maybe using a R package or write some code in some other > languages if it is not too difficult to learn. > > Any thoughts/hints please?? > > Many thanks in advance! > > HJ > > ? ? ? ?[[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.-- Kevin Wright
HJ, try something like this:
files <- list.files(pattern = "\\.(csv|CSV)$")
for (i in 1:length(files)) {
temp <- read.csv(files[i], header = FALSE)
... do whatever you want with the contents of temp...
}
Bryan
***********
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University
On May 25, 2012, at 1:16 PM, HJ YAN wrote:
> Dear R users
>
>
> I am struggling from a data importing issue:
>
> I have some hundreds of csv files needed to be read into R for futher
> analysis. All those csv files are named in one of the three formats:
>
> (1) strings: e.g. London_Oxford street
> (2) Integer: e.g. 1234_5678
> (3) combined: e.g. London_1234
>
> I intend to use read.csv("xxxx_xxx.csv") but I only dealt with
> sigle documents before and if there are only no more than 20 files, I do
> not bother to search a more efficient way.
>
>
> Is there any claver way that I do not have to type in all these hundreds
> names by hand, maybe using a R package or write some code in some other
> languages if it is not too difficult to learn.
>
> Any thoughts/hints please??
>
> Many thanks in advance!
>
> HJ
>
> [[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.
For example:
myDir <- "some file path"
filenames <- list.files(myDir)
filenames <- filenames[grep("[.]csv", filenames)]
data_names <- gsub("[.]csv", "", filenames)
for(i in 1:length(filenames)) assign(data_names[i], read.csv(file.path(myDir,
filenames[i])))
?
Benjamin Nutter |??Biostatistician ? |??Quantitative Health Sciences
? Cleveland Clinic? | ?9500 Euclid Ave.? | ?Cleveland, OH 44195? |?(216)
445-1365
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Kevin Wright
Sent: Friday, May 25, 2012 2:55 PM
To: HJ YAN
Cc: r-help at r-project.org
Subject: Re: [R] Reading a bunch of csv files into R
See ?dir
Assign the value to a vector and loop over the elements of the vector.
Kevin
On Fri, May 25, 2012 at 12:16 PM, HJ YAN <yhj204 at googlemail.com>
wrote:> Dear R users
>
>
> I am struggling from a data importing issue:
>
> I have some hundreds of csv files needed to be read into R for futher
> analysis. All those csv files are named in one of the three formats:
>
> (1) strings: e.g. London_Oxford street
> (2) Integer: e.g. 1234_5678
> (3) combined: e.g. London_1234
>
> I intend to use read.csv("xxxx_xxx.csv") but I only dealt with
sigle
> documents before and if there are only no more than 20 files, I do not
> bother to search a more efficient way.
>
>
> Is there any claver way that I do not have to type in all these
> hundreds names by hand, maybe using a R package or write some code in
> some other languages if it is not too difficult to learn.
>
> Any thoughts/hints please??
>
> Many thanks in advance!
>
> HJ
>
> ? ? ? ?[[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.
--
Kevin Wright
______________________________________________
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.
==================================
Please consider the environment before printing this e-mail
Cleveland Clinic is ranked one of the top hospitals
in America by U.S.News & World Report (2010).
Visit us online at http://www.clevelandclinic.org for
a complete listing of our services, staff and
locations.
Confidentiality Note: This message is intended for use\...{{dropped:13}}
On Fri, 25 May 2012, HJ YAN wrote:> Is there any claver way that I do not have to type in all these hundreds > names by hand, maybe using a R package or write some code in some other > languages if it is not too difficult to learn.I'd put the list of commands in a shell script; e.g., # import_files.R read_csv("file1.csv", header = T, sep = ',') read_csv("file2.csv", header = T, sep = ',') etc. Then, within a R session, run source("/full/path/to/import_files.R") or, on the command line, R CMD BATCH /full/path/name/to/import_files.R You'll still need to list all the files individually unless you use a loop. I've not tried that so someone more experienced will advise you on that. Rich
Reasonably Related Threads
- Read vector as multi-dimensional data in R by row
- How to combine conditional argument and logical argument in R to create subset of data...
- Converting factor data into Date-time format
- Check results between two data.frame
- Importing multiple worksheets from one Excle/ csv file into R