nplyr
nplyr copied to clipboard
nest_select on rowwise nested dataframes fails with an uninformative error
Not sure if this is a bug or a feature request:
When used on rowwise nested tibbles, nest_select fails, as the objects passed to map become lists instead of dataframes. (see example below) It would be nice to issue an informative warning or error instead of the current one
with:
df <- tibble(group=c(1,1,2,2),parameter=c(1,2,1,2),value=c(1,2,3,4)) %>% group_by(group) %>% nest()
while
df %>% nest_select(data,value)
works fine, while
df %>% rowwise() %>% nest_select(data,value)
fails with the rather confusing error:
Error in `dplyr::mutate()`:
ℹ In argument: `data = purrr::map(data, ~dplyr::select(.x, value))`.
ℹ In row 1.
Caused by error in `purrr::map()`:
ℹ In index: 1.
ℹ With name: parameter.
Caused by error in `UseMethod()`:
! no applicable method for 'select' applied to an object of class "c('double', 'numeric')"
Hey @jkeuskamp --- this may be an unavoidable result of how the map functions need a set of objects to map over, whereas rowwise()
breaks up those sets by row. I'll have to dig more into the documentation for purrr::map()
/dplyr::rowwise()
to confirm, but for now I've added a plain-text error when trying to use a nplyr fn on a rowwise df:
library(nplyr)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> Loading required package: tidyr
df <-
tibble::tibble(group = c(1, 1, 2, 2),
parameter = c(1, 2, 1, 2),
value = c(1, 2, 3, 4)) |>
dplyr::group_by(group) |>
tidyr::nest()
df |>
nest_select(data, value)
#> # A tibble: 2 × 2
#> # Groups: group [2]
#> group data
#> <dbl> <list>
#> 1 1 <tibble [2 × 1]>
#> 2 2 <tibble [2 × 1]>
# throws better error now
df |>
ungroup() |>
rowwise() |>
nest_select(data, value)
#> Error: argument `.data` must not be a rowwise dataframe.
#> try calling `dplyr::ungroup()`
# throws error for all nplyr functions
df |>
ungroup() |>
rowwise() |>
nest_mutate(data, value = value + 1)
#> Error: argument `.data` must not be a rowwise dataframe.
#> try calling `dplyr::ungroup()`
Created on 2023-04-10 with reprex v2.0.2