0

General Goal

I have a list of files that I got from a directory:

my_list <- list.files(path = "C:\\mallet-2.0.8\\Output_Files\\")

I'm not sure what kind of object this creates. In any event, each entry in this object holds the name of a file in my original directory. For instance,

my_list[1]

returns

 [1] "BobBlumenfield_Narrative.output"

I want to then generate data frames in the following manner:

 BobBlumenfield_Narrative <- read_delim("C:/mallet-2.0.8//Output_Files/BobBlumenfield_Narrative.output", 
                                   "\t", escape_double = FALSE, trim_ws = TRUE)

Problem

I have many files to do this for. So what I need is

 VARIABLE WITH NAME IN LIST <- read_delim("C:/mallet-2.0.8//Output_Files/FILE WITH THE NAME IN LIST", 
                                   "\t", escape_double = FALSE, trim_ws = TRUE)

and for the code to then do this for every file in the list.

My Question

How do I generate a list of data frames based on the text stored in my_list?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stan Shunpike
  • 2,165
  • 3
  • 20
  • 32
  • 2
    `my_list` is a character vector. You need to put all these data.frames into an actual `list`. Typically, you'd do something like `my_list <- list.files(path = "C:\\mallet-2.0.8\\Output_Files\\", full.names = TRUE); mydata <- lapply(mylist, read_delim, sep = "\t", escape_double = FALSE, trim_ws = TRUE)`. – Roland Feb 03 '17 at 09:23
  • I agree with Roland. Plus, you can give each list element the name of the file it was read from. – talat Feb 03 '17 at 09:25
  • @Roland hmm....I tried running it and I got `Error in FUN(X[[i]], ...) : unused argument (sep = "\t")` – Stan Shunpike Feb 03 '17 at 09:29
  • reading the document `utlis::read.delim`, it seems that the `"\t"` is default for `sep`, I would try calling lapply without this argument – Imran Ali Feb 03 '17 at 09:41
  • @ImranAli `Error in structure(list(delim = delim, quote = quote, na = na, quoted_na = quoted_na, : argument "delim" is missing, with no default` – Stan Shunpike Feb 03 '17 at 09:46
  • 1
    I had assumed you use `read.delim`. I see now that you use Hadley's `read_delim` (the underscore is easily overlooked, you should specify packages you use in your example). The parameter is called `delim`, so you need to set `delim = "\t"`. Or you could use base functions ... – Roland Feb 03 '17 at 10:14
  • I honestly didn't know there was a difference. It was just the code provided when I used the import CSV button – Stan Shunpike Feb 03 '17 at 10:27
  • It may be beneficial to read through gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) on working with lists of data.frames. – lmo Feb 03 '17 at 16:23

0 Answers0