record: Record Objects

Description Usage Arguments Details Value Record Behavior See Also Examples

View source: R/record.R

Description

Create or test for record objects.

Usage

1
2
3
4
5

Arguments

...

record fields.

x

object to be converted or tested.

Details

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.

Value

record and as.record return record objects.

is.record returns TRUE or FALSE depending on whether its argument is a record.

Record Behavior

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:

  1. index operations raise an error for unknown field names;

  2. 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;

  3. with a logical subscript i, operation x[i] treats NA values in i like FALSE;

  4. subscript with a logical value does not recycle its argument;

  5. a NULL subscript is treated as a missing value;

  6. 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.

See Also

c.record, print.record, dataset

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
# 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])

patperry/r-frame documentation built on May 6, 2019, 8:34 p.m.