r programming --- merge function returns column names with .x and .y -
while merging 2 tables, can't control column names in merge result. explain situation, let me use mtcars data:
#load mtcars data.frame data(mtcars)
add new column called 'car' use merging key
mtcars$car <- row.names(mtcars)
now create 2 mutually exclusive tables.
small <- mtcars[mtcars$cyl == 4,] med.large <- mtcars[mtcars$cyl >4,]
now when left merge, should 'small' table 2 tables mutually exclusive:
merge(x = small, y = med.large, = 'car', all.x=t)
this returns 'small' table every column appears twice .x , .y extension .y columns na (since 2 tables have no common records) , looks following
car mpg.x cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x gear.x carb.x mpg.y cyl.y 1 datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 na na
how can column names once column values primary merge table in case left table ('small'). don't know how avoid .x , .y. extension?
if every column name repeated, can use
merge(x = small, y = med.large, = names(small), all.x=t)
if column names differ, can build vector of names in both with
intersect(names(small), names(med.large))
and pass by
. otherwise, if 2 data.frames share column not passed by
, you'll end .x
or .y
type subscripts.
Comments
Post a Comment