I have a two data frame, say 'df1' and 'df2'. df1 has the following column:
Date
and df2 has the following columns:
Date.1, USD.Price, Date.2, EUR.Price, Date.3, JPY.Price, Date.4, INR.Price
where Date, Date.1, Date.2, Date.3, Date.4 ... is in date format.
Now I want to merge Date.1, USD.Price with df1 based on df1$Date and df2$Date.2 as:
df3 = merge(df1, df2[,1:2], by.x = "Date", by.y = "Date.1", all = TRUE)
Then,
df4 = merge(df3, df2[,3:4], by.x = "Date", by.y = "Date.2", all = TRUE)
Then again,
df5 = merge(df4, df2[,5:6], by.x = "Date", by.y = "Date.3", all = TRUE)
Furthermore,
df6 = merge(df5, df2[,7:8], by.x = "Date", by.y = "Date.4", all = TRUE)
and so on for all 1000 such columns.
For example, lets say, I have a following dataframe:
df1:
Date
2009-10-13
2009-10-14
2009-10-16
2009-10-18
2009-10-19
2009-10-20
2009-10-21
2009-10-22
and df2:
Date.1 USD.Price Date.2 EUR.Price Date.3 JPY.Price Date.4 INR.Price
2009-10-13 21.6 NA NA NA NA NA NA
2009-10-14 21.9 2009-10-14 78.2 NA NA NA NA
2009-10-16 22.0 2009-10-16 78.5 NA NA 2009-10-16 12.2
NA NA 2009-10-18 78.9 2009-10-18 32.1 2009-10-18 12.4
NA NA NA NA 2009-10-19 32.6 2009-10-19 12.2
Then the output needs to be:
Date USD.Price EUR.Price JPY.Price INR.Price
2009-10-13 21.6 NA NA NA
2009-10-14 21.9 78.2 NA NA
2009-10-16 22.0 78.5 NA NA
2009-10-18 NA 78.9 32.1 12.4
2009-10-19 NA NA 32.6 12.2
I have got some reference: How can I merge multiple dataframes with the same column names?
But in my case column names are different as Date.1, Date.2, Date.3 etc...
Can anyone please help me out how to do this for around 1000 columns aa doing as above is not scalable for many columns?
Thanks