0

I want to get a single elemnet from the broom tidy results into an unnested data frame.

The table structure is:

> zz
# A tibble: 1,923 x 5
   sys_loc_code data                model         tidy             glance          
   <chr>        <list>              <list>        <list>           <list>          
 1 S000-001     <tibble [493 x 18]> <S3: survreg> <tibble [4 x 7]> <tibble [1 x 8]>
 2 S000-002     <tibble [32 x 18]>  <S3: survreg> <tibble [4 x 7]> <tibble [1 x 8]>

And when I apply the broom:tidy function I get the output:

> unnest(zz, tidy)
# A tibble: 7,692 x 8
   id           term         estimate std.error statistic  p.value  conf.low  conf.high
   <chr>        <chr>           <dbl>     <dbl>     <dbl>    <dbl>     <dbl>      <dbl>
 1 S000-001     (Intercept)  4226.     881.         4.80  1.61e- 6  2499.      5952.   
 2 S000-001     y              -2.08     0.438     -4.76  1.93e- 6    -2.94      -1.23 
 3 S000-001     m               2.46     0.645      3.82  1.36e- 4     1.20       3.72 
 4 S000-001     Log(scale)      3.47     0.0383    90.7   0.          NA         NA    
 5 S000-002     (Intercept)  4610.    2880.         1.60  1.09e- 1 -1035.     10255.   
 6 S000-002     y              -2.29     1.44      -1.60  1.11e- 1    -5.10       0.523
 7 S000-002     m               1.69     1.33       1.27  2.05e- 1    -0.922      4.30 
 8 S000-002     Log(scale)      2.62     0.132     19.9   5.57e-88    NA         NA    

However, I need to grab only one element from this output. In this example, only the slopes of the y term for each id (-2.08 and -2.29) with the resulting table looking like:

> unnest(zz, tidy)
# A tibble: 7,692 x 2
   id           estimate 
   <chr>        <dbl>     
 1 S000-001     -2.08     
 2 S000-002     -2.29     

the syntax tidy(x)[2,2] works as expected when x is a sinlge class S3: "survreg", but fails when applied to a nested table of lists of the same class.

Any suggestion would be appreciated. Thanks in advance.

jmuhlenkamp
  • 2,102
  • 1
  • 14
  • 37
kray
  • 377
  • 1
  • 3
  • 11

1 Answers1

0

Given that the output of unnest is a nibble, you should be able to feed it directly into a dplyr pipeline to grab what you want. Something like this:

library(dplyr)
unnest(zz, tidy) %>%
    filter(term == "y") %>%
    select(id, estimate)
jmuhlenkamp
  • 2,102
  • 1
  • 14
  • 37