R merge without duplicating columns -


i have 2 dataframes. example

require('xlsx') csvdata <- read.csv("mydata.csv") xlsdata <- read.xlsx("mydata.xlsx") 

csvdata looks this:

period  cpi     vix 1       0.029   31.740 2       0.039   32.840 3       0.028   34.720 4       0.011   43.740 5       -0.003  35.310 6       0.013   26.090 7       0.032   28.420 8       0.022   45.080 

xlsdata looks this:

period  cpi     djia 1       0.029   12176 2       0.039   10646 3       0.028   11407 4       0.011   9563 5       -0.003  10708 6       0.013   10776 7       0.032   9384 8       0.022   7774 

when merge data, cpi data duplicated, , suffix put on header, problematic (i have many more columns in real df's).

mergeddata <- merge(xlsdata, csvdata, = "period") 

mergeddata:

period  cpi.x   vix     cpi.y   djia 1       0.029   31.740  0.029   12176 2       0.039   32.840  0.039   10646 3       0.028   34.720  0.028   11407 4       0.011   43.740  0.011   9563 5       -0.003  35.310  -0.003  10708 6       0.013   26.090  0.013   10776 7       0.032   28.420  0.032   9384 8       0.022   45.080  0.022   7774 

i want merge data frames without duplicating columns same name. example, want kind of output:

period  cpi     vix     djia 1       0.029   31.740  12176 2       0.039   32.840  10646 3       0.028   34.720  11407 4       0.011   43.740  9563 5       -0.003  35.310  10708 6       0.013   26.090  10776 7       0.032   28.420  9384 8       0.022   45.080  7774 

i don't want have use additional 'by' arguments, or dropping columns 1 of df's, because there many columns duplicated in both df's. i'm looking dynamic way drop duplicated columns during merge process.

thanks!

you can skip by argument if common columns named same.

from ?merge:

by default data frames merged on columns names both have, separate specifications of columns can given by.x , by.y.

keeping in mind, following should work (as did on 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 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -