Re: Batch mode questions.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Batch mode questions.

Rich Shepard
On Wed, 25 Jul 2018, Shawn Way wrote:

> To get you start, here's a script I used to combine any number of files
> into one data.frame.
>
> library(knitr)
> library(tidyverse)
> library(xlsx)
> library(xtable)
> library(lubridate)
>
> # create a list from these files
> list.filenames<-list.files(pattern=".CSV$")  # This gets a list of all CSV files
>
> data <- list.filenames %>%
>    map(read_csv) %>%  # This reads all the files in order
>    reduce(rbind)   # This combines the data into one data frame

   Thanks, Shawn. I'm reading up on functions and will look at using a
function within either a Rscript or source'd script.

   It's about time I learn to automate repeated, tedious tasks in R.

Much appreciated,

Rich

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Batch mode questions.

David Winsemius

> On Jul 25, 2018, at 2:54 PM, Rich Shepard <[hidden email]> wrote:
>
> On Wed, 25 Jul 2018, Shawn Way wrote:
>
>> To get you start, here's a script I used to combine any number of files
>> into one data.frame.
>>
>> library(knitr)
>> library(tidyverse)
>> library(xlsx)
>> library(xtable)
>> library(lubridate)
>>
>> # create a list from these files
>> list.filenames<-list.files(pattern=".CSV$")  # This gets a list of all CSV files

It would get all the files with all caps "CSV" anywhere within the file name, but it would not get any files with an extension ".csv", nor would it exclude a file named "somethingCSV" . The pattern argument is interpreted as a regex pattern and the meaning of a period is different.

Better for this purpose would be:

list.files(pattern="\\.(csv|CSV)$")

I suppose this criticism might be platform-specific if the filesystem were caps-agnostic, but mine is not.

--
David.

>>
>> data <- list.filenames %>%
>>   map(read_csv) %>%  # This reads all the files in order
>>   reduce(rbind)   # This combines the data into one data frame
>
>  Thanks, Shawn. I'm reading up on functions and will look at using a
> function within either a Rscript or source'd script.
>
>  It's about time I learn to automate repeated, tedious tasks in R.
>
> Much appreciated,
>
> Rich
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Batch mode questions.

Rich Shepard
On Wed, 25 Jul 2018, David Winsemius wrote:

>>> # create a list from these files
>>> list.filenames<-list.files(pattern=".CSV$")  # This gets a list of all CSV files
>
> It would get all the files with all caps "CSV" anywhere within the file
> name, but it would not get any files with an extension ".csv", nor would
> it exclude a file named "somethingCSV" . The pattern argument is
> interpreted as a regex pattern and the meaning of a period is different.
>
> Better for this purpose would be:
>
> list.files(pattern="\\.(csv|CSV)$")
>
> I suppose this criticism might be platform-specific if the filesystem were
> caps-agnostic, but mine is not.

David,

   I saw that and noted that since linux separates meaning between upper- and
lower-case letters it would need to be modified is there were any *.CSV
files in the directory. Same type of problem occurs when file names have
spaces in them.

Regards,

Rich

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.