ID EN
Data Frame & Matrix

merge

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Gabungkan dua bingkai data berdasarkan kolom atau nama baris yang umum, atau lakukan operasi penggabungan database versi lain.

Syntax

R
merge(x, y, &#8230;)<p></p><p># S3 method for default
merge(x, y, &#8230;)</p><p># S3 method for data.frame
merge(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
      sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE,
      incomparables = NULL, &#8230;)</p>

Arguments

Parameter Deskripsi
x, y data frames, or objects to be coerced to one.
by, by.x, by.y specifications of the columns used for merging. See ‘Details’.
all logical; all = L is shorthand for all.x = L and all.y = L, where L is either TRUE or FALSE.
all.x logical; if TRUE, then extra rows will be added to the output, one for each row in x that has no matching row in y. These rows will have NAs in those columns that are usually filled with values from y. The default is FALSE, so that only rows with data from both x and y are included in the output.
all.y logical; analogous to all.x.
sort logical. Should the result be sorted on the by columns?
suffixes a character vector of length 2 specifying the suffixes to be used for making unique the names of columns in the result which are not used for merging (appearing in by etc).
no.dups logical indicating that suffixes are appended in more cases to avoid duplicated column names in the result. This was implicitly false before R version 3.5.0.
incomparables values which cannot be matched. See match. This is intended to be used for merging on one column, so these are incomparable values of that column.
&#8230; arguments to be passed to or from methods.

Return Value

Bingkai data. Baris-baris tersebut secara default diurutkan secara leksikografis pada kolom umum, tetapi untuk sort = FALSE berada dalam urutan yang tidak ditentukan. Kolom-kolom tersebut adalah kolom-kolom umum diikuti oleh kolom-kolom lainnya di x dan kemudian kolom-kolom di y. Jika pencocokan melibatkan nama baris, kolom karakter tambahan yang disebut Row.names ditambahkan di sebelah kiri, dan dalam semua kasus, hasilnya memiliki nama baris 'otomatis'.

Details

merge adalah fungsi umum yang metode utamanya adalah untuk bingkai data: metode default memaksakan argumennya ke bingkai data dan memanggil metode "data.frame". Secara default, bingkai data digabungkan pada kolom dengan nama yang dimiliki keduanya, tetapi spesifikasi kolom yang terpisah dapat diberikan oleh by.x dan by.y. Baris dalam dua bingkai data yang cocok dengan kolom tertentu diekstraksi, dan digabungkan. Jika ada lebih dari satu kecocokan, semua kemungkinan kecocokan masing-masing menyumbang satu baris. Untuk arti sebenarnya dari 'kecocokan', lihat kecocokan. Kolom yang akan digabungkan dapat ditentukan berdasarkan nama, nomor, atau vektor logis: nama "nama baris" atau nomor 0 menentukan nama baris. Jika ditentukan berdasarkan nama, kolom tersebut harus sesuai secara unik dengan kolom bernama di input. Jika oleh atau kedua-duanya oleh.x dan oleh.y adalah dari

Contoh

Example
R
# NOT RUN {
authors <- data.frame(
    ## I(*) : use character columns of names to get sensible sort order
    surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
    nationality = c("US", "Australia", "US", "UK", "Australia"),
    deceased = c("yes", rep("no", 4)))
authorN <- within(authors, { name <- surname; rm(surname) })
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"))

(m0 <- merge(authorN, books))
(m1 <- merge(authors, books, by.x = "surname", by.y = "name"))
 m2 <- merge(books, authors, by.x = "name", by.y = "surname")
stopifnot(exprs = {
   identical(m0, m2[, names(m0)])
   as.character(m1[, 1]) == as.character(m2[, 1])
   all.equal(m1[, -1], m2[, -1][ names(m1)[-1] ])
   identical(dim(merge(m1, m2, by = NULL)),
             c(nrow(m1)*nrow(m2), ncol(m1)+ncol(m2)))
})

## "R core" is missing from authors and appears only here :
merge(authors, books, by.x = "surname", by.y = "name", all = TRUE)


## example of using 'incomparables'
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)
merge(x, y, by = c("k1","k2")) # NA's match
merge(x, y, by = "k1") # NA's match, so 6 rows
merge(x, y, by = "k2", incomparables = NA) # 2 rows
# }

See Also

data.frame by cbind. dendrogram for a class which has a merge method.