AnnaHo
AnnaHo

Reputation: 31

How to replace 0 in multiple columns with a formula

I have a data set like this:

df <- data.frame(ID = c("01", "02", "03", "04"),
           B = c(0, 53, 100, 58),
           C = c(0, 0, 0, 14),
           PercentageInA = c(0.001, 0, 0.127, 0.005))

I want to replace the 0 value in B and C column with the formula: value in B = sum(B)*PercentageInA. I tried with mutate across but did not get what I expect.

df %>% 
  mutate(across(.cols = col_need_to_calculate,
                .fns = ~replace(., . == 0, sum(.)*PercentageInVictoria)))

col_need_to_calculate is the vector of column's names which I perform the calculation

I expect the result would be like this:

data.frame = (B = c(0.211, 53, 100, 58),
               C = c(0.014, 0, 1.778, 14))

Upvotes: 0

Views: 39

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26225

Perhaps try if_else() instead of replace(), e.g.

library(tidyverse)

df <- data.frame(ID = c("01", "02", "03", "04"),
                 B = c(0, 53, 100, 58),
                 C = c(0, 0, 0, 14),
                 PercentageInA = c(0.001, 0, 0.127, 0.005))

expected_output <- data.frame(B = c(0.211, 53, 100, 58),
                              C = c(0.014, 0, 1.778, 14))

df %>% 
  mutate(across(.cols = c(B, C),
                .fns = ~if_else(.x == 0, sum(.x)*PercentageInA, .x)))
#>   ID       B      C PercentageInA
#> 1 01   0.211  0.014         0.001
#> 2 02  53.000  0.000         0.000
#> 3 03 100.000  1.778         0.127
#> 4 04  58.000 14.000         0.005

output <- df %>% 
  mutate(across(.cols = c(B, C),
                .fns = ~if_else(.x == 0, sum(.x)*PercentageInA, .x))) %>%
  select(c(B, C))

output
#>         B      C
#> 1   0.211  0.014
#> 2  53.000  0.000
#> 3 100.000  1.778
#> 4  58.000 14.000

all_equal(output, expected_output)
#> [1] TRUE

Created on 2023-01-31 with reprex v2.0.2

Does that solve your issue?

Upvotes: 1

Related Questions