0
votes

I am exploring the tidyverse package. So I am interested in how to get the following task down in the tidy way. One can easily circumvent the problem using *apply functions.

Consider the following data

tb <-
  lapply(matrix(c("a", "b", "c")), function(x)
    rep(x, 3)) %>% unlist %>% c(rep(c(1, 2, 3), 6)) %>% matrix(ncol = 3) %>%
  as_tibble(.name_repair = ~ c("tag", "x1", "x2")) %>% type.convert()

# A tibble: 9 x 3
  tag      x1    x2
  <fct> <int> <int>
1 a         1     1
2 a         2     2
3 a         3     3
4 b         1     1
5 b         2     2
6 b         3     3
7 c         1     1
8 c         2     2
9 c         3     3

I group them using nest() function and for each group I want to apply a different function from a list of functions f_1, f_2, f_3

f_1 <- function(x)
  x[,1] + x[,2]
f_2 <- function(x)
  x[,1] - x[,2]
f_3 <- function(x)
  x[,1] * x[,2]

tb_func_attached <- 
    tb %>% group_by(tag) %>% nest() %>% mutate(func = c(f_0, f_1, f_2))

    # A tibble: 3 x 3
  tag   data             func  
  <fct> <list>           <list>
1 a     <tibble [3 x 2]> <fn>  
2 b     <tibble [3 x 2]> <fn>  
3 c     <tibble [3 x 2]> <fn> 

I try to use invoke_map to apply the functions

tb_func_attached %>% {invoke_map(.$func, .$data)}
invoke_map(tb_func_attached$func, tb_func_attached$data)

But I get the error Error in (function (x) : unused arguments (x1 = 1:3, x2 = 1:3), while the following code runs

> tb_func_attached$func[[1]](tb_func_attached$data[[1]])
  x1
1  2
2  4
3  6
> tb_func_attached$func[[2]](tb_func_attached$data[[2]])
  x1
1  0
2  0
3  0
> tb_func_attached$func[[3]](tb_func_attached$data[[3]])
  x1
1  1
2  4
3  9

But invoke_map still does not work.

So the question is, given a nested data tb_func_attached, how to apply the functions tb_func_attached$func 'rowwisely' to tb_func_attached$data?

And a side question, what is the reason for the retirement of invoke_map? It fits quitely well in the concept of vetorisation, IMHO.

Update:

The previous version dealt with single column data (tb has only tag and x1 columns) and @A. Suliman's comment provides a solution.

However when the data column in the nested tibble has a matrix structure, the code stops running again.

1
Rename val column into x.A. Suliman
@A.Suliman As simple as that? What is the reason behind?newbie
I think column names should match the function variables names, e.g. consider ?purrr::invoke_map; df <- tibble::tibble( f = c("runif", "rpois", "rnorm"), params = list( list(n = 10), list(n = 5, lambda = 10), list(n = 10, mean = -3, sd = 10) ) ) df invoke_map(df$f, df$params), params used the arguments of each function as names inside the list.A. Suliman
So if I want to use it to a nested data, my function should always have data as the input argument?newbie

1 Answers

0
votes

Use map2 to iterate over the list of functions first, and over the data column second. Like this:

tb_func_attached %>% 
  mutate(output = map2(func, data, ~ .x(.y))) %>% 
  unnest(data, output)

The output looks this way:

# A tibble: 9 x 4
  tag      x1    x2   x11
  <fct> <int> <int> <int>
1 a         1     1     2
2 a         2     2     4
3 a         3     3     6
4 b         1     1     0
5 b         2     2     0
6 b         3     3     0
7 c         1     1     1
8 c         2     2     4
9 c         3     3     9