Description Usage Arguments Details Value See Also Examples
Perform an action on each row in a dataset and combine the results.
1 2 3 4 5 |
x |
a data object. |
f |
a function to perform on each row. |
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.
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
.
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)))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.