Hello,
Please consider this simple example. This works:
tibble(one = c(12,212,43,23,545,232),
two = c(23,12,4343,43,23,43)) %>%
do(fit = lm(one ~ two, data = .))
# A tibble: 1 x 1
fit
<list>
1 <S3: lm>
However, if I try to return a tibble with one list-column containing fit
and another column containing the r-squared of the model, my code fails
tibble(one = c(12,212,43,23,545,232),
two = c(23,12,4343,43,23,43)) %>%
do(fit = lm(one ~ two, data = .),
r_square = summary(fit)$r.squared,
data.frame(fit, r_square))
Error: Arguments must either be all named or all unnamed
Call `rlang::last_error()` to see a backtrace
Another approach fails even more spectacularly
myfunc <- function(df){
fit = lm(one ~ two, data = df)
r_square = summary(fit)$r.squared
tibble(fit, r_square)
}
tibble(one = c(12,212,43,23,545,232),
two = c(23,12,4343,43,23,43)) %>%
do(myfunc(.))
# A tibble: 12 x 2
fit r_square
<list> <dbl>
1 <dbl [2]> 0.1057382
2 <dbl [6]> 0.1057382
3 <dbl [6]> 0.1057382
4 <int [1]> 0.1057382
5 <dbl [6]> 0.1057382
6 <int [2]> 0.1057382
7 <S3: qr> 0.1057382
8 <int [1]> 0.1057382
9 <list [0]> 0.1057382
10 <language> 0.1057382
11 <S3: terms> 0.1057382
12 <data.frame [6 x 2]> 0.1057382
What is wrong here?
Thanks!