ID EN
Object & Attributes

Extract

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Operator yang bekerja pada vektor, matriks, array, dan daftar untuk mengekstrak atau mengganti bagian.

Syntax

R
x[i]
x[i, j, … , drop = TRUE]
x[[i, exact = TRUE]]
x[[i, j, …, exact = TRUE]]
x$name
getElement(object, name)<p></p><p>x[i] &lt;- value
x[i, j, &#8230;] &lt;- value
x[[i]] &lt;- value
x$name &lt;- value</p>

Arguments

Parameter Deskripsi
x, object object from which to extract element(s) or in which to replace element(s).
i, j, &#8230; indices specifying elements to extract or replace. Indices are numeric or character vectors or empty (missing) or NULL. Numeric values are coerced to integer as by as.integer (and hence truncated towards zero). Character vectors will be matched to the names of the object (or for matrices/arrays, the dimnames): see ‘Character indices’ below for further details. For [-indexing only: i, j, … can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. i, j, … can also be negative integers, indicating elements/slices to leave out of the selection. When indexing arrays by [ a single argument i can be a matrix with as many columns as there are dimensions of x; the result is then a vector with elements corresponding to the sets of indices in each row of i. An index value of NULL is treated as if it were integer(0).
name A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.
drop For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.
exact Controls possible partial matching of [[ when extracting by a character vector (for most objects, but see under ‘Environments’). The default is no partial matching. Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.
value typically an array-like R object of a similar class as x.

Details

Operator-operator ini bersifat generik. Anda dapat menulis metode untuk menangani pengindeksan kelas objek tertentu, lihat InternalMethods serta [.data.frame dan [.factor. Penjelasan di sini hanya berlaku untuk metode default. Perhatikan bahwa metode terpisah diperlukan untuk fungsi penggantian [<-, [[<- dan $<-) untuk digunakan ketika pengindeksan terjadi pada sisi penugasan ekspresi. Perbedaan yang paling penting antara [, [[ dan $ adalah [ dapat memilih lebih dari satu elemen sedangkan dua lainnya memilih satu elemen. Metode default bekerja agak berbeda untuk vektor atom, matriks/array, dan untuk objek rekursif (seperti daftar, lihat is.rekursif). $ hanya valid untuk objek rekursif, dan hanya dibahas pada bagian di bawah tentang objek rekursif. Subsetting (kecuali dengan indeks kosong)

Contoh

Example
R
# NOT RUN {
x <- 1:12
m <- matrix(1:6, nrow = 2, dimnames = list(c("a", "b"), LETTERS[1:3]))
li <- list(pi = pi, e = exp(1))
x[10]                 # the tenth element of x
x <- x[-1]            # delete the 1st element of x
m[1,]                 # the first row of matrix m
m[1, , drop = FALSE]  # is a 1-row matrix
m[,c(TRUE,FALSE,TRUE)]# logical indexing
m[cbind(c(1,2,1),3:1)]# matrix numeric index
ci <- cbind(c("a", "b", "a"), c("A", "C", "B"))
m[ci]                 # matrix character index
m <- m[,-1]           # delete the first column of m
li[[1]]               # the first element of list li
y <- list(1, 2, a = 4, 5)
y[c(3, 4)]            # a list containing elements 3 and 4 of y
y$a                   # the element of y named a

## non-integer indices are truncated:
(i <- 3.999999999) # "4" is printed
(1:5)[i]  # 3

## named atomic vectors, compare "[" and "[[" :
nx <- c(Abc = 123, pi = pi)
nx[1] ; nx["pi"] # keeps names, whereas "[[" does not:
nx[[1]] ; nx[["pi"]]
# }
# NOT RUN {
## recursive indexing into lists
z <- list(a = list(b = 9, c = "hello"), d = 1:5)
unlist(z)
z[[c(1, 2)]]
z[[c(1, 2, 1)]]  # both "hello"
z[[c("a", "b")]] <- "new"
unlist(z)

## check $ and [[ for environments
e1 <- new.env()
e1$a <- 10
e1[["a"]]
e1[["b"]] <- 20
e1$b
ls(e1)

## partial matching - possibly with warning :
stopifnot(identical(li$p, pi))
op <- options(warnPartialMatchDollar = TRUE)
stopifnot( identical(li$p, pi), #-- a warning
  inherits(tryCatch (li$p, warning = identity), "warning"))
## revert the warning option:
if(is.null(op[[1]])) op[[1]] <- FALSE; options(op)
# }

See Also

names for details of matching to names and pmatch for partial matching. list array matrix. [.data.frame and [.factor for the behaviour when applied to data.frame and factors. Syntax for operator precedence and the ‘R Language Definition’ manual about indexing details.NULL for details of indexing null objects.