combining mutate with pmap

Hello everyone,

Please help me with this data

region	value	percent
AB	3796837648	 0.81 
CD	3117799610	 0.82 
EF	2542579647	 0.83 
GH	1201423920	 0.84 
IJ	2568187424	 0.85 
KL	1981999236	 0.86 
MN	4036607551	 0.87 
OP	2590640877	 0.88 
QR	4877072411	 0.89 
ST	4627840640	 1.00 
UV	2438337591	 0.91 
XY	4068669821	 0.92 
ZA	3126475945	 0.93 
BC	3209885778	 1.00 
DE	2622450723	 0.95 

I tried to make new variable (adjust), with this formula:

fungsi <- function(value, percent) {if (percent<1) {(sum(value)*0.995 - sum(value*percent)) * value/sum(percent)}
                                      else {0}
                                       }
data_final <- data %>% mutate(adjust=pmap(list(value, percent), fungsi))

Unfortunately the new variable (adjust) values that come up are "dbl".

I wish you could show me the solution. Thank you.

data_final <- data %>% 
  rowwise() %>% 
  mutate(adjust = fungsi(value, percent))

Resulting adjust in

#> # A tibble: 15 × 4
#>    region      value percent adjust   
#>    <chr>       <dbl>   <dbl> <list>   
#>  1 AB     3796837648    0.81 <dbl [1]>
#>  2 CD     3117799610    0.82 <dbl [1]>
#>  3 EF     2542579647    0.83 <dbl [1]>
#> ...

is a list column of numerical / dbl values as pmap() always returns a list. If you are after a vector, replace pmap() with pmap_dbl() . Or in this case, rather use map2_dbl():

data %>% 
  mutate(adjust = map2_dbl(value, percent, fungsi)) 
#> # A tibble: 15 × 4
#>    region      value percent  adjust
#>    <chr>       <dbl>   <dbl>   <dbl>
#>  1 AB     3796837648    0.81 3.29e18
#>  2 CD     3117799610    0.82 2.07e18
#>  3 EF     2542579647    0.83 1.29e18
#> ...

Though... what is your actual intent / desired output here? With map*- family functions (and with rowwise()) you iterate over each individual value (or pair, or row) and function is called with scalars, lengths of value & percent arguments in your function are always 1. So it doesn't make much sense to use sum() there...

In other words, if your calculation is correct, remove conditional so function could be called with vectors (columns):

library(dplyr)

fungsi <- function(value, percent) {
  adjusted <- (sum(value)*0.995 - sum(value*percent)) * value/sum(percent)
  adjusted[percent >= 1] <- 0
  adjusted
}

data |> 
  mutate(adjust = fungsi(value, percent)) 
#> # A tibble: 15 × 4
#>    region      value percent  adjust
#>    <chr>       <dbl>   <dbl>   <dbl>
#>  1 AB     3796837648    0.81 1.31e18
#>  2 CD     3117799610    0.82 1.07e18
#>  3 EF     2542579647    0.83 8.76e17
#>  4 GH     1201423920    0.84 4.14e17
#>  5 IJ     2568187424    0.85 8.85e17
#>  6 KL     1981999236    0.86 6.83e17
#>  7 MN     4036607551    0.87 1.39e18
#>  8 OP     2590640877    0.88 8.93e17
#>  9 QR     4877072411    0.89 1.68e18
#> 10 ST     4627840640    1    0      
#> 11 UV     2438337591    0.91 8.40e17
#> 12 XY     4068669821    0.92 1.40e18
#> 13 ZA     3126475945    0.93 1.08e18
#> 14 BC     3209885778    1    0      
#> 15 DE     2622450723    0.95 9.03e17

Thanks a ton Bro, the codes completely work.. your help is really useful, I really appreciate it..

Thanks Bro, I really appreciate it, your codes work, I'll try using map2_dbl too..

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.