I have N .tsv files saved in a file named "data" into my rstudio working directory and I want to find a way to import them as separated data frames at once. Below is an example when I try to do it one by one but there are too many of them and I want something faster. Also every time their total number may be different.
#read files into R
f1<-read.table(file = 'a_CompositeSources/In1B1A_WDNdb_DrugTargetInteractions_CompositeDBs_Adhesion.tsv', sep = '\t', header = TRUE)
f2<-read.table(file = 'a_CompositeSources/In1B2A_WDNdb_DrugTargetInteractions_CompositeDBs_Cytochrome.tsv', sep = '\t', header = TRUE)
I have used :
library(readr)
library(dplyr)
files <- list.files(path = "C:/Users/user/Documents/kate/data", pattern = "*.tsv", full.names = T)
tbl <- sapply(files, read_tsv, simplify=FALSE) %>%
bind_rows(.id = "id")
##Read files named xyz1111.csv, xyz2222.csv, etc.
filenames <- list.files(path="C:/Users/user/Documents/kate/data",
pattern="*.tsv")
##Create list of data frame names without the ".csv" part
names <-gsub(".tsv", "", filenames)
###Load all files
for(i in names){
filepath <- file.path("C:/Users/user/Documents/kate/data",paste(i,".tsv",sep=""))
assign(i, read.delim(filepath,
colClasses=c("factor","character",rep("numeric",2)),
sep = "\t"))
}
but only the 1st file is read.