Introduction to the R language merge function

Source: Internet
Author: User
Authors <-Data.frame (
surname = I (C ("Tukey", "Venables", "Tierney", "Ripley", "McNeil"),
Nationality = C ("Us", "Australia", "Us", "UK", "Australia"),
deceased = C ("Yes", Rep ("No", 4)))
Books <-Data.frame (
Name = I (C ("Tukey", "Venables", "Tierney",
"Ripley", "Ripley", "McNeil", "R Core"),
title = C ("Exploratory Data analysis",
"Modern applied Statistics ...",
"Lisp-stat",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
Other.author = C (Na, "Ripley", Na, na, na, na,
"Venables & Smith"))

If you want to implement a INNER join function similar to SQL, use the code
M1 <-Merge (authors, books, by.x = "surname", by.y = "name")
If you want to implement the LEFT JOIN function, use the code
M1 <-Merge (authors, books, by.x = "surname", by.y = "name", All.x=true)
Right Join function code
M1 <-Merge (authors, books, by.x = "surname", by.y = "name", All.y=true)
All Join function code
M1 <-Merge (authors, books, by.x = "surname", by.y = "name", All=true)

The summary of univariate matching is these, but for multivariable matching, such as the following two tables, it is necessary to match the case of K1,K2 two variables equal
x <-data.frame (k1 = C (na,na,3,4,5), K2 = C (1,na,na,4,5), data = 1:5)
Y <-data.frame (k1 = C (na,2,na,4,5), K2 = C (na,na,3,4,5), data = 1:5)

The matching code is the following merge (X, y, by = C ("K1", "K2")) #inner Join





> id<-c (1,2,3,4)
> name<-c ("Jim", "Tony", "Lisa", "Tom")
> score<-c (89,22,78,78)
> Student1<-data.frame (id,name)
> Student2<-data.frame (Id,score)
> Student1
ID Name
1 1 Jim
2 2 Tony
3 3 Lisa
4 4 Tom
> Student2
ID Score
1 1 89
2 2 22
3 3 78
4 4 78
> Total_student<-merge (student1,student2,by= "ID")
> total_student
ID Name Score
1 1 Jim 89
2 2 Tony 22
3 3 Lisa 78
4 4 Tom 78
> (ID&LT;-C)
> name<-c ("Jame", "Kevin", "Sunny")
> Student1<-data.frame (id,name)
> id<-c (4,5,6)
> name<-c ("Sun", "Frame", "Eric")
> Student2<-data.frame (id,name)
> Student1
ID Name
1 1 Jame
2 2 Kevin
3 3 Sunny
> Student2
ID Name
1 4 Sun
2 5 Frame
3 6 Eric
> Total<-rbind (STUDENT1,STUDENT2)
> Total
ID Name
1 1 Jame
2 2 Kevin
3 3 Sunny
4 4 Sun
5 5 Frame
6 6 Eric
> Authors <-Data.frame (
+ surname = I (C ("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
+ nationality = C ("Us", "Australia", "Us", "UK", "Australia"),
+ deceased = C ("Yes", Rep ("No", 4)))
> Authors
Surname Nationality deceased
1 Tukey US Yes
2 Venables Australia No
3 Tierney US No
4 Ripley UK No
5 McNeil Australia No
> Books <-data.frame (
+ name = I (C ("Tukey", "Venables", "Tierney",
+ "Ripley", "Ripley", "McNeil", "R Core"),
+ title = C ("Exploratory Data analysis",
+ "Modern applied Statistics ...",
+ "Lisp-stat",
+ "Spatial Statistics", "Stochastic Simulation",
+ "Interactive Data analysis",
+ "An Introduction to R"),
+ Other.author = C (Na, "Ripley", Na, na, na, na,
+ "Venables & Smith"))
>
> Books
Name Title Other.author
1 Tukey Exploratory Data analysis <NA>
2 Venables modern applied Statistics ... Ripley
3 Tierney Lisp-stat <NA>
4 Ripley Spatial Statistics <NA>
5 Ripley Stochastic Simulation <NA>
6 McNeil Interactive Data Analysis <NA>
7 R Core an Introduction to R Venables & Smith
> Authors
Surname Nationality deceased
1 Tukey US Yes
2 Venables Australia No
3 Tierney US No
4 Ripley UK No
5 McNeil Australia No
> M1 <-Merge (authors, books, by.x = "surname", by.y = "name")
> M1
Surname Nationality deceased title Other.author
1 McNeil Australia no Interactive Data analysis <NA>
2 Ripley UK no Spatial Statistics <NA>
3 Ripley UK no Stochastic Simulation <NA>
4 Tierney US No Lisp-stat <NA>
5 Tukey US Yes exploratory Data analysis <NA>
6 Venables Australia No modern applied Statistics ... Ripley
> M1 <-Merge (authors, books, by.x = "surname", by.y = "name", All.x=true)
> M1
Surname Nationality deceased title Other.author
1 McNeil Australia no Interactive Data analysis <NA>
2 Ripley UK no Spatial Statistics <NA>
3 Ripley UK no Stochastic Simulation <NA>
4 Tierney US No Lisp-stat <NA>
5 Tukey US Yes exploratory Data analysis <NA>
6 Venables Australia No modern applied Statistics ... Ripley
> M1 <-Merge (authors, books, by.x = "surname", by.y = "name", All.y=true)
> M1
Surname Nationality deceased Title
1 McNeil Australia no Interactive Data analysis
2 R Core <NA> <NA> an Introduction to R
3 Ripley UK No Spatial Statistics
4 Ripley UK No Stochastic Simulation
5 Tierney US No Lisp-stat
6 Tukey US Yes exploratory Data analysis
7 Venables Australia No modern applied Statistics ...
Other.author
1 <NA>
2 Venables & Smith
3 <NA>
4 <NA>
5 <NA>
6 <NA>
7 Ripley
> x <-data.frame (k1 = C (na,na,3,4,5), K2 = C (1,na,na,4,5), data = 1:5)
> y <-data.frame (k1 = C (na,2,na,4,5), K2 = C (na,na,3,4,5), data = 1:5)
> x
K1 K2 Data
1 NA 1 1
2 Na Na 2
3 3 NA 3
4 4 4 4
5 5 5 5
> y
K1 K2 Data
1 Na Na 1
2 2 NA 2
3 NA 3 3
4 4 4 4
5 5 5 5
> Merge (x, y, by = C ("K1", "K2"))
K1 K2 data.x Data.y
1 4 4) 4 4
2 5 5) 5 5
3 Na Na 2 1
>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.