I am trying to analyse monthly statements from last year which are in the form of CSV files. My attempted code is below. I want to import them all in one go, assign each to an object, and then filter each to get rid of some irrelevant data. Currently it throws me an error because the objects in the 'Months' list can't be found. I tried changing them to strings, but get a different error.
My ideal output is the CSV for each month listed as a dataframe object named after the month.
This is my first post so if this is poorly formatted or lacking detail, let me know what else to provide and I will edit this post. Please help this noob.
library(readr)
library(dplyr)
Months <- c(Jan, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec)
fileN <- c("2023-01-01", "2023-02-01", "2023-03-01",
"2023-04-01", "2023-05-01", "2023-06-01",
"2023-07-01", "2023-08-01", "2023-09-01",
"2023-10-01", "2023-11-01", "2023-12-01")
dir = "/R/" #I have anonymised this directory for privacy reasons
for(i in 1:12){
FileP <- paste0(dir,fileN[i],".csv")
Months[i] <- read_csv(FileP, show_col_types = F)
Months[i] <- Months[i] %>% filter(
Months[i]$`Transaction Type` != "Internal Transfer" &
Months[i]$Amount < 0)
}
When running it, this error is given:
Error in UseMethod("filter") :
no applicable method for 'filter' applied to an object of class "list" In addition: Warning message: In Months[i] <- read_csv(FileP, show_col_types = F) : number of items to replace is not a multiple of replacement length
You have to call each of the Month's elements as such:
Note that I am extracting each element of the list using
Months[[i]]and notMonths[i], as the latter returns a list of length 1, whereas the former returns the data frame corresponding to the ith element of the list.