require(plyr)
require(dplyr)
contrasts = frame_data(
~sample_1, ~sample_2,
"foo", "bar",
"te", "st")contrasts %>% adply(1, splat(function(sample_1, sample_2){
# in here sample_1 are scalar variables no indexing is required
mutate(head(iris), samples=paste(sample_1, sample_2))
}))
So far I failed to achieve the same splat-like elegance with dplyr::rowwise:
contrasts %>% rowwise %>% do({
mutate(head(iris), samples=paste(.$sample_1[1], .$sample_2[1]))
})We need dots, dollars and indexing, whereas with splat we needed nothing.
contrasts %>% rowwise %>% do(splat(function(sample_1, sample_2){
mutate(head(iris), samples=paste(sample_1, sample_2))
})(.))
## selectively import splat into current environment
splat = plyr::splat
## or simply redefine it:
splat = function (flat) {
function(args, ...) {
do.call(flat, c(args, list(...)))
}
}
It is possible to do this with purrr
in just a few lines:
require(dplyr)
require(purrr)
contrasts = frame_data(
~sample_1, ~sample_2,
"foo", "bar",
"te", "st"
)
contrasts %>%
split(seq_len(nrow(.))) %>%
map_df(~ cbind(.x, head(iris)))
you could also use an approach with nest()
and unnest()
, but that would take more lines :)
Andrew
--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+unsubscribe@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.
contrasts %>% rowwise %>% do(with(., {
mutate(head(iris), samples=paste(sample_1, sample_2))
}))
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.