I have data collected in csv files
"F:/IMGW/2010/B00300S_2010_12.csv"
"F:/IMGW/2014/B00300S_2014_12.csv"
"F:/IMGW/2015/B00300S_2015_12.csv"
"F:/IMGW/2016/B00300S_2016_12.csv"
"F:/IMGW/2017/B00300S_2017_12.csv"
"F:/IMGW/2018/B00300S_2018_12.csv"
"F:/IMGW/2019/B00300S_2019_12.csv"
"F:/IMGW/2020/B00300S_2020_12.csv"
"F:/IMGW/2021/B00300S_2021_12.csv"
"F:/IMGW/2022/B00300S_2022_12.csv"
"F:/IMGW/2023/B00300S_2023_12.csv"
"F:/IMGW/2024/B00300S_2024_06.csv"
I create a list of files as follows:
rok <- c(2010,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024)
B.list.1 <- lapply (rok, function (rok) {
dir_ls(paste0(sciezka, "/", rok), regexp = paste0("B00300S_", rok, "(.*)csv$"))
}
)
B00300S_flist <- do.call (rbind, B.list)
I read the files like this
All <- lapply(B00300S_flist,function(i){
read.csv(i, header=FALSE, sep=";", dec = ",", na.strings = "***", skipNul = TRUE)
})
B00300S_multi <- do.call (plyr::rbind.fill, All)
However, the file has changed structure starting from June 2024 and the decimal separator is a dot (instead of a comma) and has column headers.
I need to add a condition so that files that have a date of 2024_06 or later in their name are read in a new way:
read.csv(i, header=TRUE, sep=";", dec = ".", na.strings = "***", skipNul = TRUE)
Changing your code on a minimal basis, you might try a variant of
file_names = c("F:/IMGW/2010/B00300S_2010_12.csv",
"F:/IMGW/2014/B00300S_2014_12.csv",
"F:/IMGW/2015/B00300S_2015_12.csv",
"F:/IMGW/2016/B00300S_2016_12.csv",
"F:/IMGW/2017/B00300S_2017_12.csv",
"F:/IMGW/2018/B00300S_2018_12.csv",
"F:/IMGW/2019/B00300S_2019_12.csv",
"F:/IMGW/2020/B00300S_2020_12.csv",
"F:/IMGW/2021/B00300S_2021_12.csv",
"F:/IMGW/2022/B00300S_2022_12.csv",
"F:/IMGW/2023/B00300S_2023_12.csv",
"F:/IMGW/2024/B00300S_2024_06.csv")
d = as.Date(paste0(substr(file_names, 22, 28), "_01"), "%Y_%m_%d")
idx = d >= as.Date("2024-06-01")
lapply(seq(file_names), \(i) {
if(idx[i]) read.csv(file_names[i], header=TRUE, sep=";", dec=".", na.strings="***", skipNul=TRUE) else
read.csv(file_names[i], header=FALSE, sep=";", dec=",", na.strings="***", skipNul=TRUE)
})