在运行`map::purr()`函数后,将`x`替换为列名

在运行`map::purr()`函数后,将`x`替换为列名,r,dictionary,purrr,R,Dictionary,Purrr,我使用purrr:map()函数为数据集的每一列运行lm(),其中一列作为因变量 结果几乎是完美的,除了这个-我想用我运行的变量lm()替换结果中的.x 这篇文章是相关的,但我想避免创建函数 下面是使用mtcars数据集的代码。例如,我知道第一个输出的.x指的是$mpg。我不确定setNames()是否有效 library(tidyverse) library(broom) mod3 <- map(mtcars, ~ lm(mpg ~ .x, data = mtcars)) %>%

我使用
purrr:map()
函数为数据集的每一列运行
lm()
,其中一列作为因变量

结果几乎是完美的,除了这个-我想用我运行的变量
lm()
替换结果中的
.x

这篇文章是相关的,但我想避免创建函数

下面是使用mtcars数据集的代码。例如,我知道第一个输出的
.x
指的是
$mpg
。我不确定
setNames()
是否有效

library(tidyverse)
library(broom)
mod3 <- map(mtcars, ~ lm(mpg ~ .x, data = mtcars)) %>%
  map(~tidy(.x))
#> Warning in summary.lm(x): essentially perfect fit: summary may be
#> unreliable
mod3
#> $mpg
#> # A tibble: 2 x 5
#>   term         estimate std.error statistic   p.value
#>   <chr>           <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept) -5.02e-15  9.94e-16  -5.06e 0 0.0000198
#> 2 .x           1.00e+ 0  4.74e-17   2.11e16 0        
#> 
#> $cyl
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)    37.9      2.07      18.3  8.37e-18
#> 2 .x             -2.88     0.322     -8.92 6.11e-10
#> 
#> $disp
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)  29.6      1.23        24.1  3.58e-21
#> 2 .x           -0.0412   0.00471     -8.75 9.38e-10
#> 
#> $hp
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)  30.1       1.63       18.4  6.64e-18
#> 2 .x           -0.0682    0.0101     -6.74 1.79e- 7
#> 
#> $drat
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic   p.value
#>   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)    -7.52      5.48     -1.37 0.180    
#> 2 .x              7.68      1.51      5.10 0.0000178
#> 
#> $wt
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)    37.3      1.88      19.9  8.24e-19
#> 2 .x             -5.34     0.559     -9.56 1.29e-10
#> 
#> $qsec
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic p.value
#>   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept)    -5.11    10.0      -0.510  0.614 
#> 2 .x              1.41     0.559     2.53   0.0171
#> 
#> $vs
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 (Intercept)    16.6       1.08     15.4  8.85e-16
#> 2 .x              7.94      1.63      4.86 3.42e- 5

库(tidyverse)
图书馆(扫帚)
mod3%
地图(~tidy(.x))
#>总结中的警告。lm(x):基本完美匹配:总结可能是
#>不可靠
mod3
#>每加仑$
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                             
#>1(截距)-5.02e-15 9.94e-16-5.06e 0.0000198
#>2.x 1.00e+0 4.74e-17 2.11e16 0
#> 
#>$cyl
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                           
#>1(截距)37.9 2.07 18.3 8.37e-18
#>2.x-2.88 0.322-8.92 6.11e-10
#> 
#>$disp
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                           
#>1(截距)29.61.23 24.1 3.58e-21
#>2.x-0.0412 0.00471-8.75 9.38e-10
#> 
#>$hp
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                           
#>1(截距)30.1 1.63 18.4 6.64e-18
#>2.x-0.0682 0.0101-6.74 1.79e-7
#> 
#>$drat
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                            
#>1(截距)-7.52 5.48-1.37 0.180
#>2.x 7.68 1.51 5.10 0.0000178
#> 
#>$wt
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                           
#>1(截距)37.3 1.88 19.9 8.24e-19
#>2.x-5.34 0.559-9.56 1.29e-10
#> 
#>$qsec
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                          
#>1(截距)-5.11 10.0-0.510 0.614
#>2.x1.41 0.559 2.53 0.0171
#> 
#>$vs
#>#A tible:2 x 5
#>术语估计标准误差统计p值
#>                           
#>1(截距)16.61.08 15.4 8.85e-16
#>2.x7.941.634.863.42e-5

这里有一种方法

库(tidyverse)
图书馆(扫帚)
名称(mtcars)[-1]>%
设置_名称()%>%
map(~lm(如公式(paste0('mpg~',.x)),data=mtcars))%>%
映射\u dfr(,扫帚::整齐,.id=“变量”)
#>#A tibble:20 x 6
#>变量项估计标准误差统计p值
#>                                
#>1气缸(截距)37.9 2.07 18.3 8.37e-18
#>2个气缸气缸-2.88 0.322-8.92 6.11e-10
#>3显示(截距)29.6 1.23 24.1 3.58e-21
#>4显示显示-0.0412 0.00471-8.75 9.38e-10
#>5马力(截距)30.1 1.63 18.4 6.64e-18
#>6马力-0.0682马力-0.0101马力-6.74马力-1.79e马力-7马力
#>7德拉特(截距)-7.52 5.48-1.37 1.80e-1
#>8德拉特7.68 1.51 5.10 1.78e-5
#>9 wt(截距)37.3 1.88 19.9 8.24e-19
#>10重量-5.34 0.559-9.56 1.29e-10
#>11 qsec(截距)-5.11 10.0-0.510 6.14e-1
#>12 qsec qsec 1.41 0.559 2.53 1.71e-2
#>13对(截距)16.61.08 15.4 8.85e-16
#>14比7.941.634.863.42e-5
#>上午15时(截距)17.1.12 15.2 1.13e-15
#>上午16时7.241.764.1112.85e-4
#>17档(截距)5.62 4.92 1.14 2.62e-1
#>18档3.92 1.31 3.00 5.40e-3
#>19 carb(截距)25.9 1.84 14.1 9.22e-15
#>20碳水化合物-2.06 0.569-3.62 1.08e-3
由(v0.2.1.9000)于2019-02-10创建。

Hi您可以像这样使用purr::imap():

mod3%
地图(整洁)%>%

imap(~{.x[2,1]这是正确的。这也有效,但是花括号使代码稍微不那么直接
mod3 <- map(mtcars, ~ lm(mpg ~ .x, data = mtcars)) %>%
  map(tidy) %>% 
  imap( ~   {.x[2, 1] <-  .y ; return(.x)}   )