So I have a simple data frame, a bunch of names, each with ~2,000 rows of varying numeric values each. There's a function I want to use in my analysis that takes in numeric vectors only, and I want to iterate that function over each name's values in my data frame.
Using nest() creates a tibble for each name (see below). What I am trying to do is have each element be a vector, not an one-column tibble.
library(tidyverse)
df <- tibble(
name = c(rep("Adam",5), rep("Liz",5)),
value = c(1:5, 21:25)
)
df
#> # A tibble: 10 x 2
#> name value
#> <chr> <int>
#> 1 Adam 1
#> 2 Adam 2
#> 3 Adam 3
#> 4 Adam 4
#> 5 Adam 5
#> 6 Liz 21
#> 7 Liz 22
#> 8 Liz 23
#> 9 Liz 24
#> 10 Liz 25
df %>%
nest(-name)
#> # A tibble: 2 x 2
#> name data
#> <chr> <list>
#> 1 Adam <tibble [5 × 1]>
#> 2 Liz <tibble [5 × 1]>
### What I think I want:
#> # A tibble: 2 x 2
#> name data
#> <chr> <list>
#> 1 Adam <dbl [5]>
#> 2 Liz <dbl [5]>
So easy, thank you! I was fixating on using nest() (which seemed to make sense in this context).
Next question. I'm trying to use those new vectors in a map() function with a predicate function that requires an atomic vector as input, but it's throwing the error (list) object cannot be coerced to type 'double'. How do I make the predicate function grab the vector inside the list item?
# Your solution!
df2 <- df %>%
group_by(name) %>%
summarise(magic_list = list(value))
# This is what I tried next:
df2 %>%
group_by(name) %>%
mutate(outcome = map(magic_list, crqa::optimizeParam(., rhand_y, mlpar)))
#> Error in crqa::optimizeParam(., rhand_y, mlpar) :
#> (list) object cannot be coerced to type 'double'
# But this works fine (double square brackets):
crqa::optimizeParam(df2$magic_list[[1]], rhand_y, mlpar)
# This won't work, though (single square brackets):
crqa::optimizeParam(df2$magic_list[1], rhand_y, mlpar)
#> Error in min(series) : invalid 'type' (list) of argument
Two lines are completely different (hence you get different results).
When you use anonymous function (the one with ~), it'll treat .x as a vector (what you want).
When you use . in you second example, it is a shorthand for previous step from magrittr pipe (so, in your case it will be grouped dataframe, i.e., not what you want ).