ID EN
Data Frame & Matrix

Extract.data.frame

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Ekstrak atau ganti subset bingkai data.

Syntax

R
# S3 method for data.frame
[(x, i, j, drop = )
# S3 method for data.frame
[(x, i, j) <- value
# S3 method for data.frame
[[(x, ..., exact = TRUE)
# S3 method for data.frame
[[(x, i, j) <- value
<!-- % \method{$}{data.frame}(x, name) -->
# S3 method for data.frame
$(x, name) &lt;- value

Arguments

Parameter Deskripsi
x data frame.
i, j, ... elements to extract or replace. For [ and [[, these are numeric or character or, for [ only, empty. Numeric values are coerced to integer as if by as.integer. For replacement by [, a logical matrix is allowed.
name A literal character string or a name (possibly backtick quoted).
drop logical. If TRUE the result is coerced to the lowest possible dimension. The default is to drop if only one column is left, but not to drop if only one row is left.
value A suitable replacement value: it will be repeated a whole number of times if necessary and it may be coerced: see the Coercion section. If NULL, deletes the column if a single column is selected.
exact logical: see [, and applies to column names.

Return Value

Untuk [ bingkai data, daftar, atau satu kolom (dua kolom terakhir hanya jika dimensi telah dihilangkan). Jika pengindeksan matriks digunakan untuk mengekstraksi hasil vektor. Jika hasilnya berupa bingkai data, kesalahan akan terjadi jika kolom yang tidak ditentukan dipilih (karena tidak ada konsep umum tentang kolom 'hilang' dalam bingkai data). Sebaliknya jika satu kolom dipilih dan ini tidak ditentukan, hasilnya adalah NULL. Untuk [[ kolom bingkai data atau NULL (ekstraksi dengan satu indeks) atau vektor panjang satu (ext

Details

Bingkai data dapat diindeks dalam beberapa mode. Ketika [ dan [[ digunakan dengan indeks vektor tunggal (x[i] atau x[[i]]), mereka mengindeks bingkai data seolah-olah itu adalah sebuah daftar. Dalam penggunaan ini argumen drop diabaikan, dengan peringatan. Tidak ada metode data.frame untuk $, jadi x$name menggunakan metode default yang memperlakukan x sebagai daftar (dengan pencocokan sebagian nama kolom jika kecocokannya unik, lihat Ekstrak). Metode penggantian (untuk $) memeriksa nilai jumlah baris yang benar, dan mereplikasinya jika perlu. Ketika [ dan [[ digunakan dengan dua indeks (x[i, j] dan x[[i, j]]) keduanya bertindak seperti mengindeks matriks: [[ hanya dapat digunakan untuk memilih satu elemen. Perhatikan bahwa untuk setiap kolom yang dipilih, misalnya xj, biasanya (jika tidak seperti matriks), kolom yang dihasilkan akan menjadi xj[i], dan karenanya bergantung pada [ met yang sesuai

Contoh

Example
R
# NOT RUN {
sw <- swiss[1:5, 1:4]  # select a manageable subset

sw[1:3]      # select columns
sw[, 1:3]    # same
sw[4:5, 1:3] # select rows and columns
sw[1]        # a one-column data frame
sw[, 1, drop = FALSE]  # the same
sw[, 1]      # a (unnamed) vector
sw[[1]]      # the same
sw$Fert      # the same (possibly w/ warning, see ?Extract)

sw[1,]       # a one-row data frame
sw[1,, drop = TRUE]  # a list

sw["C", ] # partially matches
sw[match("C", row.names(sw)), ] # no exact match
try(sw[, "Ferti"]) # column names must match exactly

# }
# NOT RUN {
swiss[ c(1, 1:2), ]   # duplicate row, unique row names are created

sw[sw <= 6] <- 6  # logical matrix indexing
sw

## adding a column
sw["new1"] <- LETTERS[1:5]   # adds a character column
sw[["new2"]] <- letters[1:5] # ditto
sw[, "new3"] <- LETTERS[1:5] # ditto
sw$new4 <- 1:5
sapply(sw, class)
sw$new  # -> NULL: no unique partial match
sw$new4 <- NULL              # delete the column
sw
sw[6:8] <- list(letters[10:14], NULL, aa = 1:5)
# update col. 6, delete 7, append
sw

## matrices in a data frame
A <- data.frame(x = 1:3, y = I(matrix(4:9, 3, 2)),
                         z = I(matrix(letters[1:9], 3, 3)))
A[1:3, "y"] # a matrix
A[1:3, "z"] # a matrix
A[, "y"]    # a matrix
stopifnot(identical(colnames(A), c("x", "y", "z")), ncol(A) == 3L,
          identical(A[,"y"], A[1:3, "y"]),
          inherits (A[,"y"], "AsIs"))

## keeping special attributes: use a class with a
## "as.data.frame" and "[" method;
## "avector" := vector that keeps attributes.   Could provide a constructor
##  avector <- function(x) { class(x) <- c("avector", class(x)); x }
as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function(x,i,...) {
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  r
}

d <- data.frame(i = 0:7, f = gl(2,4),
                u = structure(11:18, unit = "kg", class = "avector"))
str(d[2:4, -1]) # 'u' keeps its "unit"
# }

See Also

subset which is often easier for extraction data.frame Extract.