How I can changes the values of multiple columns according to a condition of other columns in R using dplyr?

Homer Jay Simpson

I have a data frame that looks like this :

cond1 cond2 a b c d
1 0 98 89 78 89
1 0 45 54 34 56
0 0 34 233 56 67
0 0 21 12 4 78
1 1 1 1 2 89
1 1 34 4 123 2

If the values of cond1 and cond2 columns is 0 then 0 must be the columns a and b and then 1 for columns c and d.

Ideally I want a resulted data frame like this :

cond1 cond2 a b c d
1 0 98 89 78 89
1 0 45 54 34 56
0 0 0 0 1 1
0 0 0 0 1 1
1 1 1 1 2 89
1 1 34 4 123 2

How I can do this in R using dplyr package ?

library(tidyverse)
cond1 = c(1,1,0,0,1,1)
cond2 = c(0,0,0,0,1,1)
a = c(98,45,34,21,1,34)
b = c(89,54,233,12,1,4)
c = c(78,34,56,4,2,123)
d = c(89,56,67,78,89,2)
data = tibble(cond1,cond2,a,b,c,d);data
akrun

With dplyr, if there are separate values to be replaced for blocks of columns, call the across with those subset of columns and replace

library(dplyr)
data <- data %>%
   mutate(across(c(a,b), ~  .x * !(cond1==0 & cond2==0)), 
    across(c(c, d), ~ replace(.x,(cond1 == 0 & cond2 == 0), 1)))

-output

data
# A tibble: 6 × 6
  cond1 cond2     a     b     c     d
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     0    98    89    78    89
2     1     0    45    54    34    56
3     0     0     0     0     1     1
4     0     0     0     0     1     1
5     1     1     1     1     2    89
6     1     1    34     4   123     2

Or use a single across and create a condition with case_when

data %>% 
  mutate(across(a:d, ~ case_when(cond1 == 0 & cond2 == 0 ~ c(0, 1)[
      1 + any(c('c', 'd') %in% cur_column())], TRUE ~ .x)))

-output

# A tibble: 6 × 6
  cond1 cond2     a     b     c     d
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     0    98    89    78    89
2     1     0    45    54    34    56
3     0     0     0     0     1     1
4     0     0     0     0     1     1
5     1     1     1     1     2    89
6     1     1    34     4   123     2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I filter multiple columns with dplyr using string matching for the column name?

How can I simultaneously assign value to multiple new columns with R and dplyr?

How can I compare the values in one column to the values in other columns?

How to create new columns based on other columns' values using R

R dplyr create multiple columns efficiently with condition

How to change values across multiple columns using a value conversion dataframe in R with dplyr

R dplyr: adding set of columns to other set of columns using dplyr

After using table(A,B) in R, how can I order the columns and rows according to a particular ordering?

How do I return multiple columns without consider Na values and group by other columns name in R?

How can I compare two columns and print a new column containing either of the values (of the two columns) respectively according to a condition?

How to apply a summarise across multiple columns using dplyr R

R dplyr filter string condition on multiple columns

Get frequency of values for multiple columns using dplyr?

How can I create a columns with the values in other column (R)?

How to left rows with the same values in columns using dplyr package in R?

After comparing two columns, how can I remove unique values in one column with its values in other different columns in R

How can i match the values of a column according to another of a data frame in R using dplyr?

How can I match the values of a column according to another data frame in R and print a message using dplyr?

How can I filter on dplyr using multiple columns?

How can I check the values of two columns of 2 data frames for discrepancies grouped by a variable in R using Dplyr?

How can I match the values of multiple variables according to the dates of a variable of interest and summarise them alone in R using dplyr?

How I can calculate the variance across all columns in a data frame in R according to the values of another data frame using Dplyr?

Using dplyr or base R, how to efficiently fill a data frame column with values from other columns using specified conditions?

How to assign values to multiple columns using conditions for values from other multiple columns?

How can i create a new column in R that will return specific values according to the initial values in R using dplyr?

How can I create a new column with mutate function in R that is a sequence of values of other columns in R?

How can i select specific columns that start with a word according to a condition of another column in R using dplyr?

How to transfer values from multiple columns to other columns using Pandas?

How can i convert logical columns to numeric in R using dplyr?