I have a number of txt files, while each txt file has the same dimension and column names. There are more than 10 years of consecutive daily data in each txt file, and I want to calculate average daily data from the 10 years. If there is a leap year, It will ignore the February 29's value and just use the other 365 days.
I want to put the average daily values (365 values) from the many txt files in a dataframe. Here is a small example txt files.
Use a loop to read in the files and build a single data frame then use dplyr to calculate the average. Something like this (I haven't tested this).
library(tidyverse)
filenames <- list.files(pattern=".txt$") # all text files in project directory
data <- vector("list", length(filenames)) # list to store data
for (i in seq_along(filenames)){
data[[i]] <- read_csv(filenames[i]) %>% # read as a tibble
mutate(filename=filenames[i]) # add filename to data
}
data <- bind_rows(data)
avdata < - data %>%
filter(!(month==2 && day==29)) %>% # remove these rows
group_by(month, day) %>% # average for each month-day combination
summarise(avflow=mean(flow)) # average flow