Home > Software engineering >  Conditionally replace values over a dataset R
Conditionally replace values over a dataset R

Time:10-20

Background

In this data set, 1 corresponds to a positive (yes) response and 2 corresponds with a negative (no) response. The dataset has been truncated from a larger data set.

data <- structure(list(col1 = c(1, 2, 2, 1, 1, 2, NA), 
                       col2 = c(2, 2, 2, 1, 1, 2, 1), 
                       col3 = c(1, 1, 2, 1, 1, 2, 1), 
                       col4 = c(2, 2, 2, NA, 1, 2, NA), 
                       col5 = c(2, 2, 2, 1, 1, 1, 1), 
                       col6 = c(2, 1, 2, NA, 1, 1, 2), 
                       col7 = c(2, 2, 2, 1, 2, 2, 1), 
                       col8 = c(2, 2, 2, NA, 2, 2, 1), 
                       col9 = c(2, 2, 2, NA, 2, 2, NA)), row.names = c(NA, -7L), class = c("tbl_df", 
                                                                                       "tbl", "data.frame"))

The data set produces the following results using tibble().

# A tibble: 6 x 9
   col1  col2  col3  col4  col5  col6  col7  col8  col9
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     2     1     2     2     2     2     2     2
2     2     2     1     2     2     1     2     2     2
3     2     2     2     2     2     2     2     2     2
4     1     1     1     NA    1     NA    1     NA    NA
5     1     1     1     1     1     1     2     2     2
6     2     2     2     2     1     1     2     2     2
7     NA    1     1     NA    1     2     1     1     1

My Question

My question is that for all observation that have at least one YES (1) response and no NO (2) responses, recode any missing values to 2. So, in this case, observations 4 would be altered but not 7 as 7 would fit the condition set. This would produce the desired.

# A tibble: 6 x 9
   col1  col2  col3  col4  col5  col6  col7  col8  col9
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     2     1     2     2     2     2     2     2
2     2     2     1     2     2     1     2     2     2
3     2     2     2     2     2     2     2     2     2
4     1     1     1     2     1     2     1     2     2
5     1     1     1     1     1     1     2     2     2
6     2     2     2     2     1     1     2     2     2
7     NA    1     1     NA    1     2     1     1     1

CodePudding user response:

Does this work:

data[t(apply(data, 1, function(x) (1 %in% x) & !(2 %in% x) & is.na(x)))] <- 2
data
# A tibble: 7 × 9
   col1  col2  col3  col4  col5  col6  col7  col8  col9
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     2     1     2     2     2     2     2     2
2     2     2     1     2     2     1     2     2     2
3     2     2     2     2     2     2     2     2     2
4     1     1     1     2     1     2     1     2     2
5     1     1     1     1     1     1     2     2     2
6     2     2     2     2     1     1     2     2     2
7    NA     1     1    NA     1     2     1     1    NA

CodePudding user response:

Try this, it can work :

apply(data, 1, function(row){
  if (1 %in% row && !2 %in% row){ifelse(is.na(row), 2, row)}
  else{row}
})
  • Related