Merging data frames with different column names in R

Viewed 116

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!!

1 Answers

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)))
Related