ID EN
Vector & List

match

R Base 3.6.2 🇮🇩 Bahasa Indonesia

match mengembalikan vektor posisi (pertama) kecocokan argumen pertama di argumen kedua. %in% adalah antarmuka yang lebih intuitif sebagai operator biner, yang mengembalikan vektor logis yang menunjukkan apakah ada kecocokan atau tidak untuk operan kirinya.

Syntax

R
match(x, table, nomatch = NA_integer_, incomparables = NULL)<p></p><p>x %in% table</p>

Arguments

Parameter Deskripsi
x vector or NULL: the values to be matched. Long vectors are supported.
table vector or NULL: the values to be matched against. Long vectors are not supported.
nomatch the value to be returned in the case when no match is found. Note that it is coerced to integer.
incomparables a vector of values that cannot be matched. Any value in x matching a value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL.

Return Value

Sebuah vektor yang panjangnya sama dengan x. cocok: Vektor bilangan bulat yang memberikan posisi dalam tabel kecocokan pertama jika ada kecocokan, jika tidak ada kecocokan. Jika x[i] ditemukan sama dengan tabel[j] maka nilai yang dikembalikan pada posisi ke-i dari nilai yang dikembalikan adalah j, untuk j yang terkecil. Jika tidak ditemukan kecocokan, nilainya adalah nomatch. %in%: Vektor logis, yang menunjukkan apakah ada kecocokan untuk setiap elemen x: sehingga nilainya TRUE atau FALSE dan tidak pernah NA.

Details

%in% saat ini didefinisikan sebagai "%in%" <- function(x, table) match(x, table, nomatch = 0) > 0 Faktor, vektor mentah, dan daftar diubah menjadi vektor karakter, lalu x dan tabel dipaksa ke tipe umum (yang terakhir dari dua tipe dalam urutan R, logis < integer < numerik < kompleks < karakter) sebelum mencocokkan. Jika incomparable memiliki panjang positif, maka incomparable tersebut dipaksakan ke tipe umum. Pencocokan daftar berpotensi sangat lambat dan sebaiknya dihindari kecuali dalam kasus sederhana. Persisnya apa yang cocok dengan apa, sampai batas tertentu, merupakan masalah definisi. Untuk semua tipe, NA cocok dengan NA dan tidak ada nilai lainnya. Untuk nilai nyata dan kompleks, nilai NaN dianggap cocok dengan nilai NaN lainnya, namun tidak cocok dengan NA, dimana untuk x kompleks, bagian nyata dan imajiner harus cocok dengan keduanya (kecuali mengandung setidaknya satu NA).

Contoh

Example
R
# NOT RUN {
## The intersection of two sets can be defined via match():
## Simple version:
## intersect <- function(x, y) y[match(x, y, nomatch = 0)]
intersect # the R function in base is slightly more careful
intersect(1:10, 7:20)

1:10 %in% c(1,3,5,9)
sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","%")
sstr[sstr %in% c(letters, LETTERS)]

"%w/o%" <- function(x, y) x[!x %in% y] #--  x without y
(1:10) %w/o% c(3,7,12)
## Note that setdiff() is very similar and typically makes more sense:
        c(1:6,7:2) %w/o% c(3,7,12)  # -> keeps duplicates
setdiff(c(1:6,7:2),      c(3,7,12)) # -> unique values

## Illuminating example about NA matching
r <- c(1, NA, NaN)
zN <- c(complex(real = NA , imaginary =  r ), complex(real =  r , imaginary = NA ),
        complex(real =  r , imaginary = NaN), complex(real = NaN, imaginary =  r ))
zM <- cbind(Re=Re(zN), Im=Im(zN), match = match(zN, zN))
rownames(zM) <- format(zN)
zM ##--> many "NA's" (= 1) and the four non-NA's (3 different ones, at 7,9,10)

length(zN) # 12
unique(zN) # the "NA" and the 3 different non-NA NaN's
stopifnot(identical(unique(zN), zN[c(1, 7,9,10)]))

## very strict equality would have 4 duplicates (of 12):
symnum(outer(zN, zN, Vectorize(identical,c("x","y")),
                     FALSE,FALSE,FALSE,FALSE))
## removing "(very strictly) duplicates",
i <- c(5,8,11,12)  # we get 8 pairwise non-identicals :
Ixy <- outer(zN[-i], zN[-i], Vectorize(identical,c("x","y")),
                     FALSE,FALSE,FALSE,FALSE)
stopifnot(identical(Ixy, diag(8) == 1))
# }

See Also

pmatch and charmatch for (partial) string matching match.arg etc for function argument matching. findInterval similarly returns a vector of positions but finds numbers within intervals rather than exact matches. is.element for an S-compatible equivalent of %in%. unique (and duplicated) are using the same definitions of “match” or “equality” as match() and these are less strict than == e.g. for NA and NaN in numeric or complex vectors or for strings with different encodings see also above.