2

I have 3 data frames with the same amount of columns and I want to bind them together (bind them by columns on top of each other). The problem is that these data frames come from different sources and some of the columns are slightly different, for example: df1- column A- dollar amount, df2- column A-dollar amount (USD), and R doesn't know they are the same. I tried this:

colnames(df1)=colnames(df2)

It worked, but the problem is that when I try to export the merged file, each time the name of the column is different- sometimes it's 'dollar amount' and sometimes it's 'dollar amount (USD)'.

Is there something that can be done automatically? I have over 40 columns in this data frame.

Thanks!!

zx8754
  • 52,746
  • 12
  • 114
  • 209
jennyro
  • 77
  • 7

1 Answers1

0

Assuming order of columns are same, but the spelling is different. We can get columns names from the first file, then read every file and update the column names, finally rowbind them, something like below (not tested):

library(data.table)

# get list of files
myFiles <- list.files(path = "some/folder", pattern = "*.txt", full.names = TRUE)

# get column name from first file
mycols <- colnames(fread(myFiles[ 1 ]))

# loop, read all, with the same column names, then bind the list of dataframes
myData <- rbindlist(lapply(myFiles, function(i) fread(i, col.names = mycols)))
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Thanks! what needs to be written after full.names? – jennyro Apr 13 '21 at 14:26
  • @adiki sorry, fixed the full.names = TRUE, meaning get full path to the file. – zx8754 Apr 13 '21 at 15:01
  • Thanks! Before I bind the data frames together I need to sort each of them by a certain category. How do I need to change the code if I can't just write the files path? – jennyro Apr 13 '21 at 21:09