I want to check all CSV files in a folder and count its number of columns, and for whichever file has 12 columns I want to save it in another folder
I tried the following code but it doesn't work for me.
Folder <- "E:/Extracted Tables/"
files <- list.files(path = Folder, pattern = "*.csv", full.names = TRUE )
Sheet= NULL
for (i in seq_along(files)){
Sheet[i]= as.data.frame(read.csv(files[i], header = FALSE, sep =",")%>%mutate_all(as.character))
if(ncol(Sheet[i])==12){
write.csv(Sheet[i],paste("E:/Extracted Tables/SortedFiles/",Sheet[i],".csv"))
}
}
I am getting the following error:
Error in if (ncol(Sheet[i]) == 12) { : argument is of length zero
In addition: Warning message:
In Sheet[i] <- as.data.frame(read.csv(files[i], header = FALSE, :
number of items to replace is not a multiple of replacement length