I wanted to know how to pass the output of pipe operation directly into lm()
.
For example, I can pass this following yay
vector into lm()
directly.
set.seed(40)
yay = c(rnorm(15), exp(rnorm(15)), runif(20, min = -3, max = 0))
lm(yay~1)
#> Call:
#> lm(formula = yay ~ 1)
#> Coefficients:
#> (Intercept)
#> -0.09522
But when I tried something like this, it threw an error.
library(tidyverse)
library(palmerpenguins)
data("penguins")
filter_penguins <- penguins %>% filter(species == "Adelie")
filter_penguins %>%
filter(island == "Torgersen") %>%
select(bill_length_mm) %>%
pull() %>%
lm(. ~ 1)
#> Error in formula.default(object, env = baseenv()) : invalid formula
I have also tried to save the pull()
output into object and later feed it into lm()
, it works. But why the dot placeholder doesnot work this way?
Thank you very much.
This issu is that lm()
inside a pipeline consider the data given as formula
argument. Therefore, the data is missplaced. Try:
filter_penguins %>%
filter(island == "Torgersen") %>%
select(bill_length_mm) %>%
lm(data = ., pull(.) ~ 1)