0

I´m trying to merge 2 data frames that looks like this.

DF1:      
'data.frame':   82 obs. of  4 variables:
 $ hemmabutik      : int  
 $ Sales_2016_np   : int  
 $ Sales_2016_mar  : int 



DF2:       
'data.frame':   82 obs. of  4 variables:
 $ hemmabutik      : int  
 $ Sales_2016_mar  : int



enter code here

 DF3 <- merge(x = DF1, y = DF2, by = c("hemmabutik"), all=TRUE)

DF3:     
'data.frame':   82 obs. of  4 variables:
 $ hemmabutik      : int  
 $ Sales_2016_np   : int  
 $ Sales_2016_mar.x: int  
 $ Sales_2016_mar.y: int  

What I want is this:

 DF3:     
    'data.frame':   82 obs. of  4 variables:
     $ hemmabutik      : int  
     $ Sales_2016_np   : int 
     $ Sales_2016_mar: int  

And I want that the field values for "Sales_2016_mar" in DF2 should write over the values from DF1.

Anyone have a solution to this problem?

  • Please use `dput()` to share your data. – Sotos Apr 27 '16 at 09:50
  • Is the variable `Sales_2016_mar` identical for both data frames? Then just use: `merge(x = DF1, y = DF2, all=TRUE)` as the standard option of `by` is `intersect(names(x), names(y))` – bet.bom Apr 27 '16 at 09:59
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Apr 27 '16 at 10:11
  • Any ideas how to let data frame y owerwrite data frame x for this merge statement? – Sven Johansson Apr 27 '16 at 11:50

1 Answers1

0

Let's say we have two data.frames, df1 and df2:

df1 <- data.frame(a=letters[1:10], b=letters[11:20], c=LETTERS[5:14])
df2 <- data.frame(a=letters[1:10], b=letters[1:10])

Here is what you could do with the merge() function: just replace the values of b.x with the ones of b.y, rename b.x and drop b.y:

df3 <- merge(x = df1, y = df2, by = "a", all=TRUE)
df3$b.y <- df3$b.x
df3$b.y <- NULL
colnames(df3) <- c('a','b','c')

Otherwise, you can use the dplyrpackage for a simpler one-line solution:

library(dplyr)
left_join(df1, df2, by="a") %>% transmute(a, b = b.y)
Knak
  • 496
  • 3
  • 14
  • The problem is that the variable name is dynamic and is a different depending on what month & year it is. Therefore I´m looking for a syntax that can overwrite the common column/s name/s with the values from the right data frame. – Sven Johansson Apr 27 '16 at 13:51
  • In that case it might be worth trying to use `merge()`, then grep `.y` from the column names. That way you can grab the column name you are interested in and perform the replacement/dropping. – Knak Apr 27 '16 at 14:00