Aditya Singh
2014-Nov-10 13:12 UTC
[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?
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 [[alternative HTML version deleted]]
Jeff Newmiller
2014-Nov-10 17:12 UTC
[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?
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:>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 > [[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.
John McKown
2014-Nov-10 21:15 UTC
[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?
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? > 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 ? -- The temperature of the aqueous content of an unremittingly ogled culinary vessel will not achieve 100 degrees on the Celsius scale. Maranatha! <>< John McKown [[alternative HTML version deleted]]