0

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.

1
  • create a new variable for the assign function like fnames <- sub(".*(?<=\\/)(.*)(?=\\.tsv).*", "\\1", files, perl=T) Commented Sep 15, 2015 at 17:13

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"))
)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.