dplyr, mutate with non-vectorised function returning 3 values

I'm looking to mutate with a non-vectorised function returning multiple values, as illustrated here:

# Load libraries ----------------------------------------------------------
library("tidyverse")


# Define functions --------------------------------------------------------
f = function(x, y){
  x = x[1]
  y = y[1]
  return(c("x^2" = x^2, "y^2" = y^2, "diff" = x^2 - y^2))
}


# Define example data -----------------------------------------------------
d = tibble(id = str_c("id_", 1:10),
           v1 = sample(1:10),
           v2 = sample(1:10))


# Wrangle data ------------------------------------------------------------
d %>%
  rowwise %>%
  mutate(g = list(f(v1, v2))) %>%
  unnest(g)

Yielding:

# A tibble: 30 x 4
   id       v1    v2     g
   <chr> <int> <int> <dbl>
 1 id_1      2     9     4
 2 id_1      2     9    81
 3 id_1      2     9   -77
 4 id_2      1     2     1
 5 id_2      1     2     4
 6 id_2      1     2    -3
 7 id_3      7     7    49
 8 id_3      7     7    49
 9 id_3      7     7     0
10 id_4     10     1   100
# … with 20 more rows

What I was looking for was:

# A tibble: 10 x 3
   id       v1    v2   x^2   y^2  diff 
   <chr> <int> <int> <int> <int> <int>
 1 id_1      2     9     4   81    -77 
 2 id_2      1     2   etc...
 3 id_3      7     7
 4 id_4     10     1
 5 id_5      3     8
 6 id_6      4     5
 7 id_7      6     3
 8 id_8      5     4
 9 id_9      9    10
10 id_10     8     6

Suggestions?

Hi,
Have you tried unlist_wider(g) instead of unlist(g)? That should split the lists of three that are generated by your function into three new columns.

probably tidyr::unnest_wider

Excellent - Thanks a bundle! :clap:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.