0
votes

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
Can you use unnest to get out of the list-column, then filter to select the right rows? (unnest is going to require the list elements to be data frames with the same number of columns, if I remember right)Melissa Key
When asking for help, you should include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.MrFlick
There are ways around this, but it's simplest to just change the names to differentiate the two and avoid the name clash.alistaire
Wait, in the new example, to use x and y bare, you would have to be in mutate. purrr functions don't take data frames as their first parameters to enable non-standard eval like dplyr; they're just normal functions.alistaire

1 Answers

7
votes

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.