map2() function in pipe

2019-08-19 03:00发布

问题:

I got a tibble results as below

> df
# A tibble: 6 x 5
terms results              R.squared        minP  maxP
<dbl> <list>                   <dbl>       <dbl> <dbl>
1    11 <tibble [6 x 9]>         0.589 0.269       0.939
2    10 <tibble [49 x 9]>        0.589 0.181       0.999
3     9 <tibble [200 x 9]>       0.589 0.0655      1.000
4     8 <tibble [527 x 9]>       0.585 0.000154    0.997
5     7 <tibble [972 x 9]>       0.565 0.0000607   0.998
6     6 <tibble [1,273 x 9]>     0.542 0.000000977 0.998    

There are some modeling information save in the <list> tibble results, which has several columns with names like "Formula", "maxp", "R.squared" etc.

What I want to do is to find the corresponding row in df$results with the maxp equal to the minP of df.

I can get the results by map2_df(df$results, df$minP,function(x, y) filter(x, x$maxp==y))

Now, I'd like pipe this step with all other previous steps, such as

....%>% map2_df(results, minP,function(x, y) filter(x, x$maxp==y)), the ....%>% is the steps to generate the df.

Unfortunatedly, I keep getting error message Error in as_mapper(.f, ...) : object 'y' not found.

Any suggestion?

Updated:

Here is a reproducible example:

df <- tibble(x = list(data.frame(a = c(1, 2, 5)), 
data.frame(a = c(1,2,3,4,9)), 
data.frame(a = c(3, 4, 6, 8))),
y = c(5, 4, 6))

> df 
# A tibble: 3 x 2
x                        y
<list>               <dbl>
1 <data.frame [3 x 1]>     5
2 <data.frame [5 x 1]>     4
3 <data.frame [4 x 1]>     6

I can run map2_df(df$x, df$y, function(x, y) filter(x, x$a==y)), but when I am trying df %>% map2(x, y, function(x, y) filter(x, x$a==y)), I got Error in as_mapper(.f, ...) : object 'y' not found

回答1:

I fear your example may be simpler than your actual data, but it would look like this:

library(tidyverse)

df <- tibble(x = list(data.frame(a = c(1, 2, 5)), 
                      data.frame(a = c(1,2,3,4,9)), 
                      data.frame(a = c(3, 4, 6, 8))),
             y = c(5, 4, 6))

df %>% 
    mutate(x = map2(x, y, ~filter(.x, a == .y))) %>% 
    unnest()
#> # A tibble: 3 x 2
#>       y     a
#>   <dbl> <dbl>
#> 1     5     5
#> 2     4     4
#> 3     6     6

Note that .x is a data frame and .y is a vector.