Description Usage Arguments Details Value Record Behavior See Also Examples
Create or test for record objects.
1 2 3 4 5 |
... |
record fields. |
x |
object to be converted or tested. |
These functions create or test for record objects,
lists with different subscript and print behavior (see
“Record Behavior” below for details).
record creates a new record object with fields taken from the
arguments to the method. Unnamed arguments get field names taken from
the quoted arguments.
as.record converts its argument to a record object. This is a generic
function. The default implementation converts the result of
as.list to a record.
is.record tests whether its argument is a valid record object.
record and as.record return record objects.
is.record returns TRUE or FALSE depending on whether
its argument is a record.
Records behave like lists in many respects but they have different subscripting and printing behavior.
Record subscript ([, [[, and $) behavior differs from
list behavior as follows:
index operations raise an error for unknown field names;
the subscript operation x[i] for record object x
and subscript i sets the names of the result from names(i)
if they are present;
with a logical subscript i, operation x[i] treats
NA values in i like FALSE;
subscript with a logical value does not recycle its argument;
a NULL subscript is treated as a missing value;
setting a field to NULL with double subscript ([[)
does not delete the field (delete with ([) instead).
Printing and formatting a record objects via format and print
produces different output than the corresponding methods for lists.
c.record, print.record, dataset
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 | # construct a record; unnamed arguments get names from their quoted values
b <- "foo"
x <- record(a = 10, b, c = FALSE)
names(x)
# getting an unknown field raises an error
## Not run: x$d
## Not run: x[[4]]
# setting to NULL does not delete
x[[1]] <- NULL
# different printing
print(x)
# use single subscript (`[`) to delete
y <- x
y[1] <- NULL
print(y)
# index with logical treats NA like FALSE
x[c(FALSE, NA, TRUE)]
# no recycling subscripts
## Not run: x[TRUE]
# rename fields with a named subscript
x[c(first = "c", second = "b")]
x[c(first = 3, second = 2)]
x[c(one = FALSE, two = TRUE, three = TRUE)]
# NULL treated as missing
identical(x[], x[NULL])
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.