I have a tibble:
library(dplyr)
my_tib <- tibble(names = c("blah", "blah2"), data = list(1, c(2, 3)))
which looks like this:
names data
<chr> <list>
1 blah <dbl [1]>
2 blah2 <dbl [2]>
Now I'd like to extract the first element of the data column from within a mutate clause conditional on whether the first element of the data
entry is <10 or >10:
my_tib %>%
rowwise() %>%
mutate(data_min = case_when(lengths(data) == 2 ~ data[[1]],
lengths(data) == 1 & data[[1]] > 10 ~ NA_integer_,
lengths(data) == 1 & data[[1]] < 10 ~ data[[1]]))
When I run this, I get the following error:
Error in `mutate()`:
! Problem while computing `data_min = case_when(...)`.
✖ `data_min` must be size 1, not 2.
ℹ Did you mean: `data_min = list(case_when(...))` ?
ℹ The error occurred in row 2.
Run `rlang::last_error()` to see where the error occurred.
What I'd like to get is
names data data_min
<chr> <list> <int>
1 blah <dbl [1]> 1
2 blah2 <dbl [2]> 2
I had a look here but that didn't help me with my particular situation.