Reputation: 1711
I am trying to make many models using list-columns but failed.
df <- tibble(id = 1:5, df = 1:5) %>%
nest(data = df)
Only one variable named df
in data
. I want to extract the value of df
from data
, and put it to the second argument of ns()
function. I tried 3 ways but all failed.
# way 1: use df as second argument in ns()
df %>%
mutate(model = map(data, ~ lm(mpg ~ ns(disp, df), data = mtcars)))
# way 2: use . as second argument in ns()
df %>%
mutate(model = map(data, ~ lm(mpg ~ ns(disp, .), data = mtcars))))
# way 3: use .x as second argument in ns()
df %>%
mutate(model = map(data, ~ lm(mpg ~ ns(disp, .x), data = mtcars))))
How can I revise the code?
Any help will be highly appreciated!
Upvotes: 0
Views: 34
Reputation: 389012
Take df
out from .
or .x
:
library(tidyverse)
df %>%
mutate(model = map(data, ~lm(mpg ~ splines::ns(disp, .x$df), data = mtcars)))
# id data model
# <int> <list> <list>
#1 1 <tibble [1 × 1]> <lm>
#2 2 <tibble [1 × 1]> <lm>
#3 3 <tibble [1 × 1]> <lm>
#4 4 <tibble [1 × 1]> <lm>
#5 5 <tibble [1 × 1]> <lm>
Upvotes: 3