[R] Antwort: RE: Merging variables

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Tue Jun 7 08:35:18 CEST 2016


Hi Petr,

I would like to describe the data situation in brief:

I have an business warehouse dataset (referred to as BW data) containing 
sales and an ERP  customer master data dataset with additional information 
(referred to as ERP data). Though customer IDs and customer names are 
identical due to the fact that the business warehouse data is derived from 
the ERP data.  Due to selection criteria the BW data contains slightly 
more customers than the ERP data. So customer names and all other 
information is missing in the ERP data for some cases of the BW data.  If 
I merge those by customer ID variable customer names are duplicated using 
customer.x and customer.y as variable names.

As both fields contains the same contents I would have expected R to merge 
this into one variable, e. g. customer. But this is not the case.

Can I adjust the below given merge statement - which looks almost the same 
in my script - that R does the merge of the variables if they are 
identical automatically?

This is my code using left join:

-- cut --

ds_merge1 <- merge(x = ds_bw_customer_4_match, y = 
ds_erp_customer_4_match,
  by.x = "CustID", by.y = "CustID",
  all.x = TRUE, all.y = FALSE)

-- cut --

Kind regards

Georg




Von:    PIKAL Petr <petr.pikal at precheza.cz>
An:     Michael Dewey <lists at dewey.myzen.co.uk>, "G.Maubach at weinwolf.de" 
<G.Maubach at weinwolf.de>, "r-help at r-project.org" <r-help at r-project.org>, 
Datum:  06.06.2016 17:04
Betreff:        RE: [R] Merging variables



Hi Michael

it is simple

set.seed(111)
let=sample(letters[1:10],6, replace=T)
dat1<-data.frame(let=let, customer=sample(1:10,6, replace=T))
let=sample(letters[1:10],6, replace=T)
dat2<-data.frame(let=let, customer=sample(1:10,6, replace=T))
merge(dat1, dat2, by.x="let", by.y="let", all=T)

Of course you could add customer variable to by parameter but sometimes it 
is necessary to leave it out. When you have two sets of analytical results 
and you have 2 variables operator but you want to merge those sets e.g. by 
date/hour of analysis.

Regards
Petr

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Michael
> Dewey
> Sent: Monday, June 6, 2016 3:46 PM
> To: G.Maubach at weinwolf.de; r-help at r-project.org
> Subject: Re: [R] Merging variables
>
> X-Originating-<%= hostname %>-IP: [217.155.205.190]
>
> Dear Georg
>
> I find it a bit surprising that you end up with customer.x and 
customer.y. Can
> you share with us a toy example of two data.frames which exhibit this
> behaviour?
>
> On 06/06/2016 13:29, G.Maubach at weinwolf.de wrote:
> > Hi All,
> >
> > I merged two datasets:
> >
> > ds_merge1 <- merge(x = ds_bw_customer_4_match, y =
> > ds_zww_customer_4_match,
> >   by.x = "customer", by.y = "customer",
> >   all.x = TRUE, all.y = FALSE)
> >
> > R created a new dataset with the variables customer.x and customer.y.
> > I would like to merge these two variable back together. I wrote a
> > little function (code can be run) for it:
> >
> > -- cut --
> >
> > customer.x <- c("Miller", "Smith", NA,    "Bird", NA)
> > customer.y <- c("Miller",  NA,     "Doe", "Fish", NA)
> > ds_test <- data.frame(customer.x, customer.y, stringsAsFactors =
> > FALSE)
> >
> > t_merge_variables <-
> >   function(dataset,
> >            var1,
> >            var2,
> >            merged_var) {
> >
> >     # Initialize
> >     dataset[[merged_var]] = rep(NA, nrow(dataset))
> >     dataset[["mismatch"]] = rep(NA, nrow(dataset))
> >
> >     for (i in 1:nrow(dataset)) {
> >
> >       # Check 1: var1 missing, var2 missing
> >       if (is.na(dataset[[i, var1]]) &
> >           is.na(dataset[[i, var2]])) {
> >         dataset[["mismatch"]] <- 1  # var1 & var2 are missing
> >
> >       # Check 2: var1 filled, var2 missing
> >       } else if (!is.na(dataset[[i, var1]]) &
> >                  is.na(dataset[[i, var2]])) {
> >         dataset[[i, merged_var]] <- dataset[[i, var1]]
> >         dataset[["mismatch"]] <- 0
> >
> >       # Check 3: var1 missing, var2 filled
> >       } else if (is.na(dataset[[i, var1]]) &
> >                  !is.na(dataset[i, var2])) {
> >         dataset[[i, merged_var]] <- dataset[[i, var2]]
> >         dataset[["mismatch"]] <-  0
> >
> >       # Check 4: var1 == var2
> >       } else if (dataset[[i, var1]] == dataset[[i, var2]]) {
> >       dataset[[i, merged_var]] <- dataset[[i, var1]]
> >       dataset[["mismatch"]] <- 0
> >
> >       # Leftover: var1 != var2
> >       } else {
> >         dataset[[i, merged_var]] <- NA
> >         dataset[["mismatch"]] <- 2  # var1 != var2
> >       }  # end if
> >     }  # end for
> >     return(dataset)
> > }
> >
> > ds_var_merge1 <- t_merge_variables(dataset = ds_test,
> >   var1 = "customer.x",
> >   var2 = "customer.y",
> >   merged_var = "customer")
> >
> > ds_var_merge1
> >
> > -- cut --
> >
> > It is executed without error but delivers the wrong values in the
> > variable "mismatch". This variable is always 1 although it should be
> > NA, 1 or 2 respectively.
> >
> > Can you tell me why the variable is not correctly set?
> >
> > Kind regards
> >
> > Georg
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou 
určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě 
neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho 
kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi 
či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření 
smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany 
příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve 
výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za 
společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně 
zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly 
adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, 
předloženy nebo jejich existence je adresátovi či osobě jím zastoupené 
známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its 
sender. Delete the contents of this e-mail with all attachments and its 
copies from your system.
If you are not the intended recipient of this e-mail, you are not 
authorized to use, disseminate, copy or disclose this e-mail in any 
manner.
The sender of this e-mail shall not be liable for any possible damage 
caused by modifications of the e-mail or by delay with transfer of the 
email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to 
immediately accept such offer; The sender of this e-mail (offer) excludes 
any acceptance of the offer on the part of the recipient containing any 
amendment or variation.
- the sender insists on that the respective contract is concluded only 
upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter 
into any contracts on behalf of the company except for cases in which 
he/she is expressly authorized to do so in writing, and such authorization 
or power of attorney is submitted to the recipient or the person 
represented by the recipient, or the existence of such authorization is 
known to the recipient of the person represented by the recipient.



More information about the R-help mailing list