subsetting: Subsetting tibbles

Description Usage Arguments Details Examples

Description

Accessing columns, rows, or cells via $, [[, or [ is mostly similar to regular data frames. However, the behavior is different for tibbles and data frames in some cases:

Unstable return type and implicit partial matching can lead to surprises and bugs that are hard to catch. If you rely on code that requires the original data frame behavior, coerce to a data frame via as.data.frame().

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## S3 method for class 'tbl_df'
x$name

## S3 replacement method for class 'tbl_df'
x$name <- value

## S3 method for class 'tbl_df'
x[[i, j, ..., exact = TRUE]]

## S3 replacement method for class 'tbl_df'
x[[i, j, ...]] <- value

## S3 method for class 'tbl_df'
x[i, j, drop = FALSE, ...]

## S3 replacement method for class 'tbl_df'
x[i, j, ...] <- value

Arguments

x

A tibble.

name

A name or a string.

value

A value to store in a row, column, range or cell. Tibbles are stricter than data frames in what is accepted here.

i, j

Row and column indices. If j is omitted, i is used as column index.

...

Ignored.

exact

Ignored, with a warning.

drop

Coerce to a vector if fetching one column via tbl[, j] . Default FALSE, ignored when accessing a column via tbl[j] .

Details

For better compatibility with older code written for regular data frames, [ supports a drop argument which defaults to FALSE. New code should use [[ to turn a column into a vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
df <- data.frame(a = 1:3, bc = 4:6)
tbl <- tibble(a = 1:3, bc = 4:6)

# Subsetting single columns:
df[, "a"]
tbl[, "a"]
tbl[, "a", drop = TRUE]
as.data.frame(tbl)[, "a"]

# Subsetting single rows with the drop argument:
df[1, , drop = TRUE]
tbl[1, , drop = TRUE]
as.list(tbl[1, ])


# Accessing non-existent columns:
df$b
tbl$b

df[["b", exact = FALSE]]
tbl[["b", exact = FALSE]]

df$bd <- c("n", "e", "w")
tbl$bd <- c("n", "e", "w")
df$b
tbl$b


df$b <- 7:9
tbl$b <- 7:9
df$b
tbl$b

# Identical behavior:
tbl[1, ]
tbl[1, c("bc", "a")]
tbl[, c("bc", "a")]
tbl[c("bc", "a")]
tbl["a"]
tbl$a
tbl[["a"]]

Example output

[1] 1 2 3
# A tibble: 3 x 1
      a
  <int>
1     1
2     2
3     3
[1] 1 2 3
[1] 1 2 3
$a
[1] 1

$bc
[1] 4

# A tibble: 1 x 2
      a    bc
  <int> <int>
1     1     4
$a
[1] 1

$bc
[1] 4

[1] 4 5 6
NULL
Warning message:
Unknown or uninitialised column: `b`. 
[1] 4 5 6
NULL
Warning message:
`exact` ignored. 
NULL
NULL
Warning message:
Unknown or uninitialised column: `b`. 
[1] 7 8 9
[1] 7 8 9
# A tibble: 1 x 4
      a    bc bd        b
  <int> <int> <chr> <int>
1     1     4 n         7
# A tibble: 1 x 2
     bc     a
  <int> <int>
1     4     1
# A tibble: 3 x 2
     bc     a
  <int> <int>
1     4     1
2     5     2
3     6     3
# A tibble: 3 x 2
     bc     a
  <int> <int>
1     4     1
2     5     2
3     6     3
# A tibble: 3 x 1
      a
  <int>
1     1
2     2
3     3
[1] 1 2 3
[1] 1 2 3

tibble documentation built on July 23, 2021, 5:06 p.m.