Reputation: 75
I have a large list with 6937 elements. Each dataframe inside the list represents a station that collects daily hydrological data.
In my file folder, each txt file is named with a code that represents the location of the station, and I need to keep the file name when I import the data into R.
My question is, how do I import all these txt files into R while conserving their names?
Name them (as explained here) once they are imported is impossible (as I mentioned there are 6000+ elements in the list)
This is how I imported each txt file into R:
library(tidyverse)
library(readtext)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(purrr)
# Import all data
txt_files_ls <- paste("C:/Users/obarresi/Desktop/doc osvaldo/ana_data_acquisition/data_flow-ANA/All",
list.files(path = "C:/Users/obarresi/Desktop/doc osvaldo/ana_data_acquisition/data_flow-ANA/All",
pattern = "*.txt"), sep = "/")
txt_files_df_list <- vector("list", length(txt_files_ls))
txt_files_df_list <- lapply(txt_files_ls,
function(x){data.frame(read.table(file = x, header = F,
sep ="", colnames(x)))})
#After all the cleaning process
head(list[[1]])
#> day month year flow date
37252 28 12 2001 35 2001-12-28
37253 29 12 2001 34 2001-12-29
37254 30 12 2001 37 2001-12-30
37255 31 12 2001 34 2001-12-31
37256 1 1 2002 34 2002-01-01
37257 2 1 2002 36 2002-01-02
Upvotes: 1
Views: 99
Reputation: 75
So I came up with a simple answer :
files <- list.files(path="C:/Users/obarresi/Desktop/doc osvaldo/ana_data_acquisition/data_flow-ANA/All", pattern=".txt")
names(txt_files_df_list) <- files
Upvotes: 1