Home > database >  Make R function that return several columns and append them to the existing data frame
Make R function that return several columns and append them to the existing data frame

Time:08-24

I want to expand my existing function which returns a column of values.

library(tidyverse)

some_function <- function(cost, rbf_sigma){
  cost   rbf_sigma
}

tibble(cost = 1:5,
       rbf_sigma = 11:15) %>% 
  mutate(val1 = map2_dbl(cost, rbf_sigma, some_function))

So, this function returns:

# A tibble: 5 x 3
   cost rbf_sigma  val1
  <int>     <int> <dbl>
1     1        11    12
2     2        12    14
3     3        13    16
4     4        14    18
5     5        15    20

And this is a function that returns two columns:

some_function2 <- function(cost, rbf_sigma){
  value1 <- cost   1
  value2 <- rbf_sigma   3
  tibble(value1, value2)
}

So, I want to apply this function as before, more or less like this (this of course produces an error and the part that I am stuck):

tibble(cost = 1:5,
       rbf_sigma = 11:15) %>% 
  mutate(val1 = map2_dbl(cost, rbf_sigma, some_function)$value1,
         val2 = map2_dbl(cost, rbf_sigma, some_function)$value1)

I want the result to be something like this:

# A tibble: 5 x 4
   cost rbf_sigma  val1  val2
  <int>     <int> <dbl> <dbl>
1     1        11     2    14
2     2        12     3    15
3     3        13     4    16
4     4        14     5    17
5     5        15     6    18

Any idea how to do this? Thanks and appreciate any help.

CodePudding user response:

How about using unnest():

library(purrr)
library(dplyr)
library(tidyr)
some_function2 <- function(cost, rbf_sigma){
  value1 <- cost   1
  value2 <- rbf_sigma   3
  tibble(value1, value2)
}

tibble(cost = 1:5,
       rbf_sigma = 11:15) %>% 
  mutate(out = map2(cost, rbf_sigma, some_function2)) %>% 
  unnest(out)
#> # A tibble: 5 × 4
#>    cost rbf_sigma value1 value2
#>   <int>     <int>  <dbl>  <dbl>
#> 1     1        11      2     14
#> 2     2        12      3     15
#> 3     3        13      4     16
#> 4     4        14      5     17
#> 5     5        15      6     18

Created on 2022-08-22 by the reprex package (v2.0.1)

CodePudding user response:

Could be done this way too:

some_function2 <- function(cost, rbf_sigma){
  value1 <- cost   1
  value2 <- rbf_sigma   3
  tibble(value1, value2)
}
tibble(cost = 1:5, rbf_sigma = 11:15) %>% tibble(., some_function2(.$cost, .$rbf_sigma))
# A tibble: 5 × 4
   cost rbf_sigma value1 value2
  <int>     <int>  <dbl>  <dbl>
1     1        11      2     14
2     2        12      3     15
3     3        13      4     16
4     4        14      5     17
5     5        15      6     18

Or using %$% from magrittr with some changes to the function output:

library(magrittr)
some_function2 <- function(cost, rbf_sigma){
  value1 <- cost   1
  value2 <- rbf_sigma   3
  tibble(cost, rbf_sigma, value1, value2)
}
tibble(cost = 1:5, rbf_sigma = 11:15) %$% some_function2(cost, rbf_sigma)
# A tibble: 5 × 4
   cost rbf_sigma value1 value2
  <int>     <int>  <dbl>  <dbl>
1     1        11      2     14
2     2        12      3     15
3     3        13      4     16
4     4        14      5     17
5     5        15      6     18

Or create a function that takes in data frame/tibble instead:

some_function2 <- function(data){
  value1 <- data$cost   1
  value2 <- data$rbf_sigma   3
  tibble(cost = data$cost, rbf_sigma = data$rbf_sigma, value1, value2)
}
tibble(cost = 1:5, rbf_sigma = 11:15) %>% some_function2(.)
# A tibble: 5 × 4
   cost rbf_sigma value1 value2
  <int>     <int>  <dbl>  <dbl>
1     1        11      2     14
2     2        12      3     15
3     3        13      4     16
4     4        14      5     17
5     5        15      6     18
  • Related