I have multiple .csv files in my directory which don't have a column name. So while reading them without header gives error
Error in match.names(clabs, names(xi)) : names do not match previous names.
So for that reason, I want to append column names to those csv files and combine them all to one single dataframe, but I'm not able to add a column name to those multiple csv file while reading them. File names are like test_abc.csv , test_pqr.csv, test_xyz.csv etc.
here is what I tried
temp = list.files(pattern="*.csv")
read_csv_filename <- function(filename){
ret <- read.csv(filename,header = F)
ret$city <- gsub(".*[_]([^.]+)[.].*", "\\1", filename)
ret
}
df_all <- do.call(rbind,lapply(temp,read_csv_filename))
How do I add header here to every file while reading?
This is a names that I want to add while reading
colnames = c("Age","Gender","height","weight")
Any suggestion?
read.csv(..., col.names = c("Age","Gender","height","weight"))? Or do I get your question wrong?