I have 8 text files with no headers all saved, my goal is to read them all in and combine them into one file. I also want to add the column names but keep getting an error. My code so far is:
txt_files_ls = list.files(path=mypath, pattern="*.txt")
txt_files_df <- lapply(txt_files_ls, function(x) {read.table(file = x, header = F, sep ="\t",colnames(x))})
combined_df <- do.call("rbind", lapply(txt_files_df, as.data.frame))
colnames(combined_df)<-c("INSUR","POLICY","STREET","STREETPRED","STREETNAME","STREETTYPE","STREETPOSTD","STREETADD2","CITY","STATE","ZIP","ZIP4","EFFDATE","POLTYPE","PREM","FILL", "BVAMOUNT","Full","COUNT")
I keep receiving this error: Error in names(x) <- value : 'names' attribute [19] must be the same length as the vector [1]
But I know that those are all the columns and they are identical.
I am also trying to have a column that IDs where the text file came from, but that is a separate issue. Thank you for anyone that sees this