replacing `.x` with column name after running `map::purrr()` function

前端 未结 2 1954
孤独总比滥情好
孤独总比滥情好 2021-01-15 13:49

I run lm() for every column of a dataset with one of the column as the dependent variable, using purrr:map() function.

The results are alm

2条回答
  •  春和景丽
    2021-01-15 14:54

    Hi you can use purrr::imap() like so:

    mod3 <- map(mtcars, ~ lm(mpg ~ .x, data = mtcars)) %>%
      map(tidy) %>% 
      imap( ~   {.x[2, 1] <-  .y ; return(.x)}   )
    

    imap sends two things to the function/ formula : .x the item and .y which is either the name of the item (name in this case) or the index. I had to wrap everything in {} in this case to get the assignment to work

提交回复
热议问题