1

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

Brian Gal
  • 13
  • 2
  • Can you print `combined_df` and `colnames(combined_df)`? – otwtm Apr 28 '20 at 13:54
  • I can print combined_df, after looking at it, it appears to have put all text in one column instead of separating.. How would i mitigate that? the delimiter is white space but when i change it to white space the error is Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 2 did not have 107 elements – Brian Gal Apr 28 '20 at 14:12

1 Answers1

0

Try this:

txt_files_ls <- paste(mypath, list.files(path = mypath, pattern = "*.txt"), sep = "/")
txt_files_df_list <- vector("list", length(txt_files_ls))
txt_files_df_list <- lapply(txt_files_ls, 
                       function(x){data.frame(read.table(file = x, header = F,
                                                         sep ="\t",colnames(x)))})
combined_df <- setNames(do.call("rbind", txt_files_df_list),
                        c("INSUR", "POLICY","STREET","STREETPRED","STREETNAME",
                          "STREETTYPE","STREETPOSTD","STREETADD2","CITY","STATE",
                          "ZIP","ZIP4","EFFDATE","POLTYPE","PREM","FILL", "BVAMOUNT",
                          "Full","COUNT")) 
hello_friend
  • 5,682
  • 1
  • 11
  • 15