at: An operator to extract a named member of a list that is in a...

Description Usage Arguments Details Value Examples

Description

The name 'dfat' comes from 'df' for data frames, and ' This overwrites the slotOP http://www.inside-r.org/r-doc/base/slotOp, which is only for S4 objects.

Usage

1
"@"(x, key)

Arguments

x

A list of lists, where the sub-lists should have named members.

key

Either a string that is the name of the attribute to extract, or an integer index of the attribute to extract.

Details

Imagine you have a data.frame where a variable/column is a list of lists, and the lists have named members, e.g.,

df$itinary <- list( list(from="NYC", to="LA", price=1200, via="train"), list(from="LA", to="SF", price="unknown"), ...)

You want to get the "from" value of itinary as a vector. You can do

df$itinary or

It is slightly more than syntactic sugar for

sapply(x, function(m) { m[["from"]] })

We extended the idea in several ways:

1). We define a in-fix operator

2). We added error handling, in the case of bad indecies, etc.

Value

A vector of the attribute, in the same class as the attribute, with unsuccessful conversions (non-atomic or recursive values such as lists) filled by NAs. Note that the attribute may be a vector, which would result in a matrix if x is a vector. In this version we will return NA, just to keep the return value a strict vector.

Examples

1
2
3
4
library(daft)

df$itinary%AT%"from"
%AT%(df$itinary, "via")

garyfeng/dfat documentation built on May 16, 2019, 5:42 p.m.