Hello Posit Community,
I would like to know how to read multiple txt files that contain variables such as year, month, day, temperature max, temperature min, precipitation. Then create a dataframe with all files and finally add new column with the name of each city.
The name of the city is contained in the txt file after a certain string for each file ("Name of time serie"). In this example, I would extract the word "Barcelona" and then fill a new column with this city name. Then I want to do the same for each txt file
Txt structure example
#######
tex_file: 1
Name of time serie: Barcelona
text_line1
text_line2
text_line3
(ten or eleven of descriptive lines information. For each txt file could be different)
year month day PPTX TX TN
1950 1 1 0 12.9 2.1
1950 1 2 0 14.9 3.1
1950 1 3 0 13.5 4.1
(data continue until 2022)...
#######
I am trying with this code but I dont know how to continue creating the newcolumn with the name of the city for each txt file (60)
files_to_read <-list.files("C:/Users/RStudio/project")
read_a_file <- function(x) {
fread(
file = file.path( "C:/Users/RStudio/project", x),
select = c("year", "month", "day", "PPTX", "TX", "TN"),
)
myresults<-purrr::map_dfr(files_to_read,
read_a_file)