[R] 1. What function to use to read all the files in a directory to a vector in R code? 2. What function to use to coerce character string into numeric?

Marc Girondot marc_grt at yahoo.fr
Tue Nov 11 10:36:09 CET 2014


Le 10/11/2014 22:15, John McKown a écrit :
> On Mon, Nov 10, 2014 at 7:12 AM, Aditya Singh <aps6dl at yahoo.com> wrote:
>
>> Hi,
>> I have 2 queries:
>> 1. What function to use to read all the files in a directory to a vector
>> in R code?

In the package phenology, a function read_folder can be used to read all 
the files at one time into a list. After you can use unlist():

Description
To create a list, the syntax is

datalist<-read_folder(folder=".", read=read.delim, header=FALSE)

Return NULL with a warning if the folder does not exist or is empty. The 
names of the elements of the list are the filenames.

Usage
read_folder(folder = try(file.choose(), silent = TRUE), wildcard = 
"*.*", read = read.delim, ...)

Arguments
folder Where to search for files; can be or a file path or a folder path
wildcard Define which files are to be read (examples: "*.*", "*.xls", 
"essai*.txt")
read Function used to read file. Ex: read.delim or read.xls from gdata 
package
... Parameters send to the read function

Sincerely,

Marc Girondot


>> 2. What function to use to coerce character string into numeric?
>> As a help to others, I figured out to use setwd("C:/....") to set working
>> directory!
>> Aditya
>>
>
> ​First, please don't post in HTML. It is contrary to forum policy. Thanks.
>
> Answer 1: you can get a list of the names of all the files using the
> list.files() function. Do a ?list.files for more information.
>
> Answer 2: If I understand your question correctly, then I'd use
> as.numeric() .
>
> Suggestion 1: Get & read a good book on R programming. These were very
> basic questions. I'd strongly suggest "Advanced R" by Hadley Wickham. You
> can order if Amazon.com, or read it (for free!) here:
> http://adv-r.had.co.nz/
>>
> ​Another good one is "The Art of R Programming" by Norman Matloff. This
> later one is what I used, but that's mainly because Hadley's book hadn't
> been written (or maybe published) when I was first learning R. ​
>
> ​Suggestion 2: If you haven't already, I would strongly recommend getting &
> installing RStudio. It is free (as in beer, which is a curious phrase
> because beer isn't usually free). http://www.rstudio.com>



More information about the R-help mailing list