Skip to content
Prev 358931 / 398502 Next

create function for compare two dataframe.

Hi

You said

When I asked this question, I used indent for readability.
So maybe it was added extra space.
I executed same command. It has no extra space.
'data.frame':    3 obs. of  4 variables:
 $ ID  : Factor w/ 3 levels "ID1","ID2","ID3": 1 2 3
 $ VAL1: int  2 0 0
 $ VAL2: int  2 3 2
 $ VAL3: int  3 3 4
'data.frame':    3 obs. of  4 variables:
 $ ID  : Factor w/ 3 levels "ID1","ID2","ID3": 1 2 3
 $ VAL1: int  0 0 0
 $ VAL2: int  2 2 2
 $ VAL3: int  3 3 2

That is why dput(s1) is always preferable for exchanging data instead of text in ***HTML formated*** mail.

Cheers
Petr


From: Hiroyuki Sato [mailto:hiroysato at gmail.com]
Sent: Monday, February 29, 2016 12:04 PM
To: PIKAL Petr; r-help at r-project.org
Subject: Re: [R] create function for compare two dataframe.


Hello Petr.

Thank you for replying.
Your step is better than my step!.

I added one step
ID variable df1 df2
1 ID1     VAL1   2   0
5 ID2     VAL2   3   2
9 ID3     VAL3   4   2

This output is what I wanted!!.


P.S.

When I asked this question, I used indent for readability.
So maybe it was added extra space.
I executed same command. It has no extra space.
'data.frame':  3 obs. of  4 variables:
 $ ID  : Factor w/ 3 levels "ID1","ID2","ID3": 1 2 3
 $ VAL1: int  2 0 0
 $ VAL2: int  2 3 2
 $ VAL3: int  3 3 4
'data.frame':  3 obs. of  4 variables:
 $ ID  : Factor w/ 3 levels "ID1","ID2","ID3": 1 2 3
 $ VAL1: int  0 0 0
 $ VAL2: int  2 2 2
 $ VAL3: int  3 3 2

Best regards.


2016?2?29?(?) 18:16 PIKAL Petr <petr.pikal at precheza.cz<mailto:petr.pikal at precheza.cz>>:
Hi

You does not need to create function, you can use functions already available.
You presented second table without names.
ID VAL1 VAL2 VAL3
1   ID1    2    2    3
2   ID2    0    3    3
3   ID3    0    2    4
ID VAL1 VAL2 VAL3
1   ID1    0    2    3
2   ID2    0    2    3
3   ID3    0    2    2

You need to add a column in which you specify data frame and merge them
Now you need to reshape your data
Using ID, dat as id variables
ID dat variable value
1    ID1 df1     VAL1     2
2    ID2 df2     VAL1     0
3    ID2 df1     VAL1     0
4    ID3 df2     VAL1     0
5    ID3 df1     VAL1     0
6    ID1 df2     VAL1     0
7    ID1 df1     VAL2     2
8    ID2 df2     VAL2     2
9    ID2 df1     VAL2     3
10   ID3 df2     VAL2     2
11   ID3 df1     VAL2     2
12   ID1 df2     VAL2     2
13   ID1 df1     VAL3     3
14   ID2 df2     VAL3     3
15   ID2 df1     VAL3     3
16   ID3 df2     VAL3     2
17   ID3 df1     VAL3     4
18   ID1 df2     VAL3     3

And cast the new structure.
ID variable df1 df2
1    ID1     VAL1   2  NA
2    ID1     VAL2   2  NA
3    ID1     VAL3   3  NA
4    ID2     VAL1   0   0
5    ID2     VAL2   3   2
6    ID2     VAL3   3   3
7    ID3     VAL1   0   0
8    ID3     VAL2   2   2
9    ID3     VAL3   4   2
10   ID1     VAL1  NA   0
11   ID1     VAL2  NA   2
12   ID1     VAL3  NA   3

This was the point that I was rather surprised but I found a reason. Your ID variable does not have 3 but four values - although ID1 looks the same, in one there is an extra space, therefore you have different ID1 in s1 from ID1 in s2.

That is why it is recommended to use dput() for exchanging data with others.
'data.frame':   6 obs. of  5 variables:
 $ ID  : Factor w/ 4 levels "  ID1","  ID2",..: 1 2 2 3 3 4
 $ VAL1: int  2 0 0 0 0 0
 $ VAL2: int  2 2 3 2 2 2
 $ VAL3: int  3 3 3 2 4 3
 $ dat : chr  "df1" "df2" "df1" "df2" ...
[1]   ID1   ID2   ID3
Levels:   ID1   ID2   ID3
[1] ID1     ID2   ID3
Levels:   ID2   ID3 ID1
'data.frame':   3 obs. of  5 variables:
 $ ID  : Factor w/ 3 levels "  ID1","  ID2",..: 1 2 3
 $ VAL1: int  2 0 0
 $ VAL2: int  2 3 2
 $ VAL3: int  3 3 4
 $ dat : chr  "df1" "df1" "df1"
'data.frame':   3 obs. of  5 variables:
 $ ID  : Factor w/ 3 levels "  ID2","  ID3",..: 3 1 2
 $ VAL1: int  0 0 0
 $ VAL2: int  2 2 2
 $ VAL3: int  3 3 2
 $ dat : chr  "df2" "df2" "df2"
Cheers
Petr
________________________________
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.

________________________________
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.