match in dependence of 2 columns

classic Classic list List threaded Threaded
7 messages Options
Mat
Reply | Threaded
Open this post in threaded view
|

match in dependence of 2 columns

Mat
Hello,

i want to match a column of one data.frame to another, my problem is, that i only want to match the data, if 2 columns are equal.

i have a example:

data.frame1
                  cu.nr.         name              value
A                 1                 Evo                 100
B                 1                 Mer                 80
C                 2                 Ford                50

And now i want to match the value to data.frame 2 in dependence of cu.nr. and name.

data.frame2
                  cu.nr.         name        
A                 33                 Evo              
B                 1                 Mer            
C                 1                 Pold              

as you can see, there is only one row with equal cu.nr. and name. How can i match the data as desired?
The solution has to look like this one:

                  cu.nr.         name          Value
A                 33                 Evo              
B                 1                 Mer           80  
C                 1                 Pold      

thanks.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,
Try this:
dat1<- read.table(text="
                  cu.nr.         name              value
A                 1                 Evo                 100
B                 1                 Mer                 80
C                 2                 Ford                50
",sep="",header=TRUE,stringsAsFactors=F)
dat2<- read.table(text="
                  cu.nr.         name        
A                 33                 Evo              
B                 1                 Mer            
C                 1                 Pold
",sep="",header=TRUE,stringsAsFactors=F)  
library(plyr)
join(dat1,dat2,by=c("cu.nr.","name"),type="right")
  cu.nr. name value
#1     33  Evo    NA
#2      1  Mer    80
#3      1 Pold    NA
   A.K.  
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

John Kane
In reply to this post by Mat
?merge

If I have not reversed the data sets this should work
With data sets called dat1 and dat2

merge(dat1,dat2, all.y = TRUE)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Wed, 13 Feb 2013 06:39:57 -0800 (PST)
> To: [hidden email]
> Subject: [R] match in dependence of 2 columns
>
> Hello,
>
> i want to match a column of one data.frame to another, my problem is,
> that i
> only want to match the data, if 2 columns are equal.
>
> i have a example:
>
> data.frame1
>                   cu.nr.         name              value
> A                 1                 Evo                 100
> B                 1                 Mer                 80
> C                 2                 Ford                50
>
> And now i want to match the value to data.frame 2 in dependence of cu.nr.
> and name.
>
> data.frame2
>                   cu.nr.         name
> A                 33                 Evo
> B                 1                 Mer
> C                 1                 Pold
>
> as you can see, there is only one row with equal cu.nr. and name. How can
> i
> match the data as desired?
> The solution has to look like this one:
>
>                   cu.nr.         name          Value
> A                 33                 Evo
> B                 1                 Mer           80
> C                 1                 Pold
>
> thanks.
>
> Mat
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

Rui Barradas
In reply to this post by Mat
Hello,

Try the following.


data.frame1 <- read.table(text = "
                   cu.nr.         name              value
A                 1                 Evo                 100
B                 1                 Mer                 80
C                 2                 Ford                50
", header = TRUE)


data.frame2 <- read.table(text = "
                   cu.nr.         name
A                 33                 Evo
B                 1                 Mer
C                 1                 Pold
", header = TRUE)

merge(data.frame2, data.frame1, all.x = TRUE)



Hope this helps,

Rui Barradas

Em 13-02-2013 14:39, Mat escreveu:

> Hello,
>
> i want to match a column of one data.frame to another, my problem is, that i
> only want to match the data, if 2 columns are equal.
>
> i have a example:
>
> data.frame1
>                    cu.nr.         name              value
> A                 1                 Evo                 100
> B                 1                 Mer                 80
> C                 2                 Ford                50
>
> And now i want to match the value to data.frame 2 in dependence of cu.nr.
> and name.
>
> data.frame2
>                    cu.nr.         name
> A                 33                 Evo
> B                 1                 Mer
> C                 1                 Pold
>
> as you can see, there is only one row with equal cu.nr. and name. How can i
> match the data as desired?
> The solution has to look like this one:
>
>                    cu.nr.         name          Value
> A                 33                 Evo
> B                 1                 Mer           80
> C                 1                 Pold
>
> thanks.
>
> Mat
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Mat
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

Mat
In reply to this post by arun kirshna
thank you, this code works:

library(plyr)
join(dat1,dat2,by=c("cu.nr.","name"),type="right")

but my dat2 frame has a lot of columns, which i don't want to match.
How can i say, that only the column "value" should be match to dat1 ?

Thank you.

Mat
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

Rui Barradas
Hello,

You can do something like this:


join(dat1, dat2[, c("cu.nr", "name", "value")],
by=c("cu.nr.","name"),type="right")


Hope this helps,

Rui Barradas
Em 14-02-2013 07:03, Mat escreveu:

> thank you, this code works:
>
> library(plyr)
> join(dat1,dat2,by=c("cu.nr.","name"),type="right")
>
> but my dat2 frame has a lot of columns, which i don't want to match.
> How can i say, that only the column "value" should be match to dat1 ?
>
> Thank you.
>
> Mat
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405p4658508.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: match in dependence of 2 columns

arun kirshna
In reply to this post by Mat
HI,

If the dataset is similar to this:
dat1<- read.table(text="
                  cu.nr.         name              value 
A                 1                 Evo                 100
B                 1                 Mer                 80 
C                 2                 Ford                50 
",sep="",header=TRUE,stringsAsFactors=F)
dat2<- read.table(text="
                  cu.nr.         name      value2 value3       
A                 33                 Evo   60       120    
B                 1                 Mer    40        90
C                 1                 Pold   20        30  
",sep="",header=TRUE,stringsAsFactors=F) 
library(plyr)
join(dat1,subset(dat2,select=c("cu.nr.","name")),by=c("cu.nr.","name"),type="right")
#  cu.nr. name value
#1     33  Evo    NA
#2      1  Mer    80
#3      1 Pold    NA
#or
join(dat1,dat2[,c("cu.nr.","name")],by=c("cu.nr.","name"),type="right")


A.K.



----- Original Message -----
From: Mat <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, February 14, 2013 2:03 AM
Subject: Re: [R] match in dependence of 2 columns

thank you, this code works:

library(plyr)
join(dat1,dat2,by=c("cu.nr.","name"),type="right")

but my dat2 frame has a lot of columns, which i don't want to match.
How can i say, that only the column "value" should be match to dat1 ?

Thank you.

Mat




--
View this message in context: http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405p4658508.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
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.


______________________________________________
[hidden email] mailing list
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.