The []
and [[]]
operators for safeframe
objects behaves like for
regular data.frame
or tibble
, but check that labelled variables are not
lost, and takes the appropriate action if this is the case (warning, error,
or ignore, depending on the general option set via lost_labels_action()
) .
Usage
# S3 method for class 'safeframe'
x[i, j, drop = FALSE]
# S3 method for class 'safeframe'
x[i, j] <- value
# S3 method for class 'safeframe'
x[[i, j]] <- value
# S3 method for class 'safeframe'
x$name <- value
Arguments
- x
a
safeframe
object- i
a vector of
integer
orlogical
to subset the rows of thesafeframe
- j
a vector of
character
,integer
, orlogical
to subset the columns of thesafeframe
- drop
a
logical
indicating if, when a single column is selected, thedata.frame
class should be dropped to return a simple vector, in which case thesafeframe
class is lost as well; defaults toFALSE
- value
the replacement to be used for the entries identified in
x
- 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.
See also
lost_labels_action()
to set the behaviour to adopt when labels are lost through subsetting; default is to issue a warningget_lost_labels_action()
to check the current the behaviour
Examples
if (require(dplyr) && require(magrittr)) {
## create a safeframe
x <- cars %>%
make_safeframe(
speed = "Miles per hour",
dist = "Distance in miles"
) %>%
mutate(result = if_else(speed > 50, "fast", "slow")) %>%
set_labels(result = "Ticket")
x
## dangerous removal of a labelled column setting it to NULL issues warning
x[, 1] <- NULL
x
x[[2]] <- NULL
x
x$age <- NULL
x
}
#> Warning: The following labelled variables are lost:
#> speed - Miles per hour
#> Warning: The following labelled variables are lost:
#> result - Ticket
#>
#> // safeframe object
#> dist
#> 1 2
#> 2 10
#> 3 4
#> 4 22
#> 5 16
#> 6 10
#> 7 18
#> 8 26
#> 9 34
#> 10 17
#> 11 28
#> 12 14
#> 13 20
#> 14 24
#> 15 28
#> 16 26
#> 17 34
#> 18 34
#> 19 46
#> 20 26
#> 21 36
#> 22 60
#> 23 80
#> 24 20
#> 25 26
#> 26 54
#> 27 32
#> 28 40
#> 29 32
#> 30 40
#> 31 50
#> 32 42
#> 33 56
#> 34 76
#> 35 84
#> 36 36
#> 37 46
#> 38 68
#> 39 32
#> 40 48
#> 41 52
#> 42 56
#> 43 64
#> 44 66
#> 45 54
#> 46 70
#> 47 92
#> 48 93
#> 49 120
#> 50 85
#>
#> labelled variables:
#> dist - Distance in miles