Home > Mobile >  Conditionally replace all values based on value of 1 column in R
Conditionally replace all values based on value of 1 column in R

Time:10-21

Background

The data set is given below for reproducibility

data <- structure(list(rest1 = c(1, 1, 0, 1, 1, 1, 0, 1, 0, 1), 
               rest2 = c(1, 0, 1, 0, 0, 1, 1, 0, 0, 0), 
               rest3 = c(1, 0, 0, 0, 0, 1, 0, 1, 0, 0), 
               rest4 = c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0), 
               rest5 = c(1, 1, 0, 0, 0, 1, 0, 1, 0, 1), 
               rest6 = c(0, 0, 1, 0, 0, 0, 1, 0, 1, 0)), 
          class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))

The output is given below:

A tibble: 10 x 6
   rest1 rest2 rest3 rest4 rest5 rest6
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1     1     1     1     1     0
 2     1     0     0     0     1     0
 3     0     1     0     0     0     1
 4     1     0     0     0     0     0
 5     1     0     0     0     0     0
 6     1     1     1     1     1     0
 7     0     1     0     0     0     1
 8     1     0     1     0     1     0
 9     0     0     0     0     0     1
10     1     0     0     0     1     0

My question

Based on the values of column sleep 6, there needs to be changes made. Given the variable rest6 is equal to 1, the other variables rest1-rest5 need to be changed to 0. Here, variables 3 and 7 need to be fixed.

The desired output is below:

  rest1 rest2 rest3 rest4 rest5 rest6
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1     1     1     1     1     0
 2     1     0     0     0     1     0
 3     0     0     0     0     0     1
 4     1     0     0     0     0     0
 5     1     0     0     0     0     0
 6     1     1     1     1     1     0
 7     0     0     0     0     0     1
 8     1     0     1     0     1     0
 9     0     0     0     0     0     1
10     1     0     0     0     1     0

Previous Attempts

I have attempted to do so using my basic knowledge of R. My logic is if rest6 is equal to 1 and the observations are equal to 1, then set to 0, else we return the original value. However, this has not worked and I am a little unsure/not as proficient in R as of deliberate.

data <- ifelse(data$rest6 == 1 & data[,c(2:5) == 1], 
               0, 
               data[,c(2:6)])

Another attempt I have tried to use a function() to identify where to place the values.

Thank you for your help.

CodePudding user response:

A simple base R solution may be to isolate all those in which rest6 == 1 and change all values in the relevant columns to 0:

data[data$rest6 %in% 1, 1:5] <- 0

Output:

  rest1 rest2 rest3 rest4 rest5 rest6
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1     1     1     1     1     0
 2     1     0     0     0     1     0
 3     0     0     0     0     0     1
 4     1     0     0     0     0     0
 5     1     0     0     0     0     0
 6     1     1     1     1     1     0
 7     0     0     0     0     0     1
 8     1     0     1     0     1     0
 9     0     0     0     0     0     1
10     1     0     0     0     1     0

CodePudding user response:

In tidyverse, a simple solution would be to loop across columns rest1 to rest5, and use case_when to replace the values that correspond to 1 in rest6 to 0

library(dplyr)
data <- data %>%
  mutate(across(rest1:rest5,
    ~  case_when(rest6 == 1 ~  0, TRUE ~ .x)))

-output

data
# A tibble: 10 × 6
   rest1 rest2 rest3 rest4 rest5 rest6
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1     1     1     1     1     0
 2     1     0     0     0     1     0
 3     0     0     0     0     0     1
 4     1     0     0     0     0     0
 5     1     0     0     0     0     0
 6     1     1     1     1     1     0
 7     0     0     0     0     0     1
 8     1     0     1     0     1     0
 9     0     0     0     0     0     1
10     1     0     0     0     1     0

CodePudding user response:

data.table solution

library(data.table)
setDT(data)

data[rest6 == 1, 1:5 := 0]
  • Related