purrr
nest map_dbl :
library(tidyverse)
df %>%
rowwise %>%
nest(-blob) %>%
mutate(s = map_dbl(data, FUN)) %>%
unnest
. -, rowwise , .
, nest , , FUN ( tibbles vs data.frames!). rowwise, exclude:D
, map_dbl FUN . map_dbl map_* , (.. ).
unnest .
purrrlyr
purrrlyr "", dplyr purrr, by_row .
df FUN :
df %>%
by_row(..f = FUN, .to = "s", .collate = "cols")
! :
exclude B C D blob s
<chr> <dbl> <dbl> <dbl> <chr> <dbl>
1 B 1 3 1 fd 4
2 B 0 4 1 fs 5
3 D 0 9 0 sa 9
, , :
..f= ,.to= , s.collate= .collate , . FUN , "cols" "rows"
purrrlyr...
, by_row, ! purrr , . . microbenchmark:
library(microbenchmark)
mbm <- microbenchmark(
purrr.test = df %>% rowwise %>% nest(-blob) %>%
mutate(s = map_dbl(data, FUN)) %>% unnest,
purrrlyr.test = df %>% by_row(..f = FUN, .to = "s", .collate = "cols"),
rowwise.test = df %>%
rowwise %>%
do({
result = as_tibble(.)
result$s = FUN(result)
result
}),
group_by.test = df %>%
group_by(1:n()) %>%
do({
result = as_tibble(.)
result$s = FUN(result)
result
}),
sapply.test = {df$s <- sapply(1:nrow(df), function(x) FUN(df[x,]))},
times = 1000
)
autoplot(mbm)

, purrrlyr , do rowwise group_by(1:n()) (. @konvas), sapply. , , . purrr , , , , . .