Reputation: 52850
I want this in R:
fsC=[read.table(x) for x in Sys.glob('./Trial7/*.csv')]
i.e. trying to read the content of each file to a separate vector where vectors belong to a data structure.
Python
[file(x, 'r').read() for x in glob.glob('./Trial7/*.csv')]
or better actually
[file(x, 'r') for x in glob.glob('./Trial7/*.csv')]
but I think you got the point...
Upvotes: 1
Views: 2406
Reputation: 121127
You have two questions here. First, "given a vector of filenames, how do you read those files into R?".
Here's your list of filenames
trial7_files <- Sys.glob("Trial7/*.csv")
#if you prefer to specify the names using regular expressions, try
trial7_files <- dir("Trial7", "\\.csv$")
As previously noted, lapply
is the best way of reading in the files.
fsC <- lapply(trial7_files, read.csv)
This gives you a list of data frames, and leads to your next question. "How do you combine a list of data frames with the same columns into one data frame?"
The standard way to do this is with do.call
and rbind
. First, it's useful to make a note of how many rows there are in each dataset.
n_records <- sapply(fsC, nrow)
fsC <- do.call(rbind, fsC)
That's your problem solved, though you probably want a column telling you which file each row came from.
fsC$source <- rep(trial7_files, n_records)
Upvotes: 7
Reputation: 15278
Use sapply to "map" vectors:
sapply(Sys.glob('./Trial7/*.csv'), read.table) -> fsc
Upvotes: 5