Skip to content

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?

5 messages · Aditya Singh, Jeff Newmiller, John McKown +2 more

#
Hi,
I have 2 queries:
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?
As a help to others, I figured out to use setwd("C:/....") to set working directory!
Aditya
#
1. There is no built-in function to do that, but it can be done if you learn the basics of R [1]. For one thing, there is no assurance that all files in a directory will fit into vectors.. Most data fit better into data frames, and some data like XML need to be stored in lists of lists. So your first task is to work this out and use the str function to make sure you understand how your data for one file looks when imported into R variables.

Once you figure out how your files can be read in (perhaps with read.csv or read.table?) then you can use list.files to get a vector of filenames, and lapply to create a list of contents of files. You may need to re-read the discussions of lists and indexing in [1] again to see how to get the pieces out of the list. If all your files actually do have the same data frame structure, then you might find it useful to merge the list into a single data frame. Something like:

fnames <- list.files( pattern="\\.csv$" )
dtalist <- lapply(fnames,function(fn){read.csv(fn,stringsAsFactors=FALSE)})
alldta <- do.call(rbind,dtalist)

2. Read help file ?numeric.

You should also read the Posting Guide, which among other advice mentions that you should post using plain text format on this list rather than HTML (since we don't see what you see if you use the latter).

[1] Introduction to R, supplied with the software.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On November 10, 2014 5:12:10 AM PST, Aditya Singh <aps6dl at yahoo.com> wrote:
#
On Mon, Nov 10, 2014 at 7:12 AM, Aditya Singh <aps6dl at yahoo.com> wrote:

            
?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 ?
#
On Mon, Nov 10, 2014 at 4:15 PM, John McKown
<john.archie.mckown at gmail.com> wrote:
[snip]
Rstudio is free "as in free speech" as well, as far as I can tell.
Though there is a non-free (in both senses) version as well.

Best,
Ista
#
Le 10/11/2014 22:15, John McKown a ?crit :
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