plyr
has a very convenient option, .inform = TRUE
, which lets user know which pieces in the split-apply-combine process fails.
Is there an equivalent way to know which piece fails with dplyr
's group_by
?
A minimal example: group 1 in the following df
is the group causing problem, but dplyr
error message doesn't reveal that information.
df <- data.frame(group = c(1, 1, 2, 2, 2))
df %>% group_by(group) %>% mutate(value = c(10, 10, 10))
# Error: incompatible size (3), expecting 2 (the group size) or 1
I don't know a way within dplyr
, so this may be a bit orthogonal to what you're looking for, but here's a potential approach using split
and purrr::possibly
.
library(purrr); library(dplyr)
set_3_tens <- function(df) { df %>% mutate(value = c(10,10,10)) }
df %>%
split(.$group) %>%
map(
possibly(
set_3_tens, otherwise = "Problem in group"))
$`1`
[1] "Problem in group"
$`2`
group value
1 2 10
2 2 10
3 2 10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.