Reputation: 349
I'm trying to read in a bunch of .tsv files from a folder ('Test'), and I got this far:
files <- list.files("Test", pattern="*.tsv", full.names=TRUE)
for (i in 1:length(files)) assign(files[i], read.delim(files[i], na.strings = c(
"FAILED", "ERROR"))
)
This works, however the data frames it creates includes the file path and extension (in this case Test/
and .tsv
resulting in a data frame named Test/100_1.tsv
). I've been playing around with the code for a few hours trying to get it to name each data frame by the necessary information only (e.g., 100_1
). If anyone has any suggestions, this novice would greatly appreciate the help.
Upvotes: 0
Views: 78
Reputation: 1268
If you're just trying to get 100_1.tsv
all you need to do is set full.names = FALSE
and preset your working directory rather than call it in list.files
.
E.g.
setwd("C:/Your/Working/Directory/Test")
files <- list.files(pattern="*.tsv")
for (i in 1:length(files)) assign(files[i], read.delim(files[i], na.strings = c(
"FAILED", "ERROR"))
)
full.names
defaults to FALSE
.
Upvotes: 0
Reputation: 7248
I would suggest loading these as a list, rather than into the global namespace with assign
.
Something like
files <- list.files("Test", pattern="*.tsv", full.names=TRUE)
names(files) <- files
all.data <- lapply(files, function(fle) {
read.delim(fle, na.strings=c("FAILED", "ERROR"))
})
will yield a list keyed by filename, which will be much easier to deal with.
Upvotes: 1
Reputation: 1053
I would make a label object that contains the names you would like to apply to your dataframes. The label object I made assumes you always have the same size names. Should do what you are looking for though.
files <- list.files("Test", pattern="*.tsv", full.names=TRUE)
dflabels <- substr(files,5,nchar(files)-4)
for (i in 1:length(files)) assign(dflabels[i], read.delim(files[i], na.strings = c(
"FAILED", "ERROR"))
)
Upvotes: 0