Explain with an example how to merge dataframes in R.

1.0K    Asked by SanjanaShah in Data Science , Asked on Dec 12, 2019
Answered by Nitin Solanki

Let us create two data frames author and books

We will be creating dataframes that contain names of authors along with the surnames and the nation they belong. Later on, we will be merging the dataframes

## use character columns of names to get sensible sort order

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"))

Now let us merge the two dataframes and name it m1

(m1 <- merge(authors, books, by.x = "surname", by.y = "name"))



Your Answer

Interviews

Parent Categories