R uses POSIX extended regex, so * means 0 or more times, and . means any character except newline. Your regex as written should most likely be .*\\.csv, however, csv$ should be sufficient.
How to remove NAs in multiple CSV files in a folder
I have a folder containing 15000 CSV files and I need to remove all NAs in all those files.
• 3,004 views
•
link
2 answers
if files do not have headers, try running the following command (take a back up of your files and create a directory by name "output", before you proceed):
$ parallel 'grep -v "NA" {} > output/new_{.}.csv' ::: *.csv
if files have headers, install tsv-utils and run following command:
$ parallel 'keep-header {} -- grep -v "NA" > output/new_{.}.csv' ::: *.csv
• 0 views
•
link
Probably this job more suitable for bash, but here is using R: find all files, keep only complete cases (remove any row that has NA), then output with renamed filename.
library(data.table)
for(i in list.files("path/to/files", pattern = ".*\\.csv", full.names = TRUE)){
d <- fread(i)
fwrite(d[ complete.cases(d), ], file = paste0(i, ".clean.csv"))
}
• 0 views
•
link
• 0 views
•
link
This code doesn't produce a ".clean.csv" file
• 0 views
•
link
Log in to answer this question.
Do you want to remove any rows/columns with NA values, or replace NA with something? For this question you may want to include an example of what one of the files looks like.
Remove NAs meaning:
I need to remove rows with NAs
You have to write a script to iterate on each csv to drop rows that contains NA.