就结果本身而言,你做的很好:
foo <- foo %>% mutate(mods = map2(X1, X2, fit_lm))
str(foo, max.level = 1)
# 'data.frame': 21 obs. of 3 variables:
# $ X1 : Factor w/ 6 levels "cyl","disp","drat",..: 5 5 5 5 5 5 1 1 1 1 ...
# $ X2 : Factor w/ 6 levels "cyl","disp","drat",..: 1 2 4 3 6 5 2 4 3 6 ...
# $ mods:List of 21
问题很简单
class(foo)
# [1] "data.frame"
以及像
lm
全班都印着
print.data.frame
. 因此,我们可以用
print.tbl
,我们只需要转换
foo
到
tibble
:
foo <- as.tbl(foo)
foo
# A tibble: 21 x 3
# X1 X2 mods
# <fct> <fct> <list>
# 1 mpg cyl <S3: lm>
# 2 mpg disp <S3: lm>
# 3 mpg hp <S3: lm>
# 4 mpg drat <S3: lm>
# 5 mpg wt <S3: lm>
# 6 mpg qsec <S3: lm>
# 7 cyl disp <S3: lm>
# 8 cyl hp <S3: lm>
# 9 cyl drat <S3: lm>
# 10 cyl wt <S3: lm>
# ⦠with 11 more rows