Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain list names after applying map

Tags:

r

purrr

I'm passing a list to map and want to return a data.frame object with incorporated names.

For example:

library(tidyverse)
library(broom)

mtcars %>%
    split(.$vs) %>%
    map_df(~ tidy(lm(mpg ~ cyl, .)))

         term  estimate std.error statistic      p.value
1 (Intercept) 36.926733  3.690822 10.005017 2.727754e-08
2         cyl -2.728218  0.490297 -5.564419 4.272958e-05
3 (Intercept) 41.940000  5.778467  7.257981 1.003636e-05
4         cyl -3.802500  1.240052 -3.066404 9.781943e-03

How can I extract names (vs group) within map and add them as an additional column in the result like this:

         term  estimate std.error statistic      p.value GROUP
1 (Intercept) 36.926733  3.690822 10.005017 2.727754e-08 0
2         cyl -2.728218  0.490297 -5.564419 4.272958e-05 0
3 (Intercept) 41.940000  5.778467  7.257981 1.003636e-05 1
4         cyl -3.802500  1.240052 -3.066404 9.781943e-03 1
like image 662
simon321 Avatar asked Apr 18 '18 07:04

simon321


People also ask

How do I add place names to a geopandas map?

In this post we look at how we can use the Ordnance Survey’s Open Names dataset to add place names to our GeoPandas maps. The first step is to generate a Shapefile of place names, as points, in GM. The data is downloaded as a set of CSV files covering grid squares in Great Britain.

What is list retainall() method in Java with examples?

List retainAll () Method in Java with Examples Last Updated : 02 Jan, 2019 This method is used to retain all the elements present in the collection from the specified collection into the list.

How do you retain elements in a list in Java?

List retainAll() Method in Java with Examples. This method is used to retain all the elements present in the collection from the specified collection into the list. Syntax: Parameters: This method has only argument, collection of which elements are to be retained in the given list.

How to rename a field name in ArcGIS?

With ArcGIS 10.1 you are able to rename the field name in ArcCatalog (table properties), if your data source is a FileGeodatabase. Show activity on this post. The most reliable way to do this in ArcMap is to use the Table to Excel (Conversion) tool, making sure the Use field alias as column header checkbox is checked:


2 Answers

Use the .id parameter, which map_df will pass along to dplyr::bind_rows:

library(purrr)

mtcars %>%
    split(.$vs) %>%
    map_dfr(~broom::tidy(lm(mpg ~ cyl, .)), .id = 'GROUP')
#>   GROUP        term  estimate std.error statistic      p.value
#> 1     0 (Intercept) 36.926733  3.690822 10.005017 2.727754e-08
#> 2     0         cyl -2.728218  0.490297 -5.564419 4.272958e-05
#> 3     1 (Intercept) 41.940000  5.778467  7.257981 1.003636e-05
#> 4     1         cyl -3.802500  1.240052 -3.066404 9.781943e-03
like image 81
alistaire Avatar answered Sep 27 '22 22:09

alistaire


Here is one option with group_by/nest/unnest

mtcars %>% 
  group_by(GROUP = vs) %>% 
  nest(-GROUP) %>%
  mutate(out = map(data, ~ tidy(lm(mpg ~ cyl, .x))) ) %>%
  select(-data) %>%
  unnest
# A tibble: 4 x 6
#   GROUP  term        estimate std.error statistic      p.value    
#   <dbl> <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#1  0    (Intercept)    36.9      3.69      10.0  0.0000000273
#2  0    cyl           - 2.73     0.490    - 5.56 0.0000427   
#3  1.00 (Intercept)    41.9      5.78       7.26 0.0000100   
#4  1.00 cyl           - 3.80     1.24     - 3.07 0.00978     
like image 43
akrun Avatar answered Sep 27 '22 21:09

akrun