do: Operating on Dataset Rows

Description Usage Arguments Details Value See Also Examples

View source: R/do.R

Description

Perform an action on each row in a dataset and combine the results.

Usage

1
2
3
4
5
do(x, f)
## Default S3 method:
do(x, f)
## S3 method for class 'dataset'
do(x, f)

Arguments

x

a data object.

f

a function to perform on each row.

Details

The do function calls code f using each row of x as a set of arguments. It then casts the result to a record with as.record and binds the records together into a dataset.

Value

If x has no rows, then the result is NULL. Otherwise, the result is a dataset with the same keys as x with rows equal to the result of applying f to each row in x.

See Also

group, lapply.

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
# perform an action on each row
x <- dataset(n = 1:10)
do(x, function(n) 10 * n)

# named result
do(x, function(n) c(a = 10 * n))

# multiple results
do(x, function(n)
   record(a =  10 * n,
          b = -10 * n,
          c = "foo"))

# multiple columns
y <- dataset(count = c("one", "two", "three"),
             fruit = c("banana", "banana", "orange"))
do(y, function(c, f) c(fun = paste(c, f)))

# split into groups
x <- group(mtcars, gear, cyl)

# perform an action on each group
do(x, function(g)
   record(n    = nrow(g),
          mean = mean(g$mpg),
          sd   = sd(g$mpg)))

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