My csv files come in the format 0.csv, 1.csv up to 50.csv. Now I want to read them into R in numerical order but R continues to import it with 0.csv, 1.csv, 10.csv, 11.csv etc. What is the correct regex sucht that it reads the csv files ordered numerically?.Here is a minimal example:
ldf <- list()
list_Candidates<- dir(pattern = "[[:digit:]].csv")
creates the list of all the csv files in the Directory
for (k in 1:length(list_Candidates)){
ldf[[k]] <- fread(list_Candidates[k], sep=",")
loop over the length of the list and read each csv into the list
ldf_Header = colnames(ldf[[k]])
ldf[[k]] = ldf[[k]][,1:(length(ldf[[k]])-1)] # deletes the last column
colnames(ldf[[k]]) = ldf_Header[-1]
This part deletes the last column (this is to correct a bug within the fread function)
ldf[[k]] <- ldf[[k]] %>% mutate(Candidate=k-1) # creates an additional column and assigns Candidate number
}
The final part creates a new column in each dataframe which corresponds to the number of the csv file. Therefore it is curical that they are imported in the correct order in the first place. Thank you! :)