How to read all files in one directory into R at once?

r

Solution

There are three steps:

- Fetch all file names via `list.files`

- Use `lapply` to read all files in a list

- Use `do.call` to `rbind` all data into a single data frame or matrix

The code:

nm <- list.files(path="path/to/file")
do.call(rbind, lapply(nm, function(x) read.table(file=x)[, 2]))

Subsetting with `[]` is arbitrary, this example is for the second columns only.

Problem

I have folder, which contain around 200 .txt files. I want to read all of the files and select second column of each of them and put them in one matrix. (rbind()) is there any command to read all files at once ? I want to use : ``` data<-read.table ("", header= T, sep=",") ```

Original source

Related problems