R merge without duplicating columns

You can also index your specific column of interest by name. This is useful if you just need a single column/vector from a large data frame.

Period <- seq(1,8)
CPI <- seq(11,18)
VIX <- seq(21,28)
DJIA <- seq(31,38)
Other1 <- paste(letters)[1:8]
Other2 <- paste(letters)[2:9]
Other3 <- paste(letters)[3:10]

df1<- data.frame(Period,CPI,VIX)
df2<- data.frame(Period,CPI,Other1,DJIA,Other2,Other3)

merge(df1,df2[c("Period","DJIA")],by="Period") 

> merge(df1,df2[c("Period","DJIA")],by="Period")
  Period CPI VIX DJIA
1      1  11  21   31
2      2  12  22   32
3      3  13  23   33
4      4  14  24   34
5      5  15  25   35
6      6  16  26   36
7      7  17  27   37
8      8  18  28   38

You can skip the by argument if the common columns are named the same.

From ?merge:

By default the data frames are merged on the columns with names they both have, but separate specifications of the columns can be given by by.x and by.y.

Keeping that in mind, the following should work (as it did on your sample data):

merge(csvData, xlsData)
#   Period    CPI   VIX  DJIA
# 1      1  0.029 31.74 12176
# 2      2  0.039 32.84 10646
# 3      3  0.028 34.72 11407
# 4      4  0.011 43.74  9563
# 5      5 -0.003 35.31 10708
# 6      6  0.013 26.09 10776
# 7      7  0.032 28.42  9384
# 8      8  0.022 45.08  7774

Tags:

Merge

R

Dataframe