Home > Back-end >  How to change 1, 2, 3, 4 to 0, 1 across multiple columns?
How to change 1, 2, 3, 4 to 0, 1 across multiple columns?

Time:09-22

There is a tibble that contains number 1 to 4.

library(tidyverse)

# reproducible data
sample_tbl <- structure(list(X1 = c(4L, 4L, 1L, 4L, 1L, 4L, 2L, 3L, 2L, 4L), 
X2 = c(1L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 3L, 2L), X3 = c(4L, 
3L, 3L, 3L, 2L, 2L, 1L, 1L, 4L, 2L), X4 = c(1L, 4L, 3L, 2L, 
3L, 4L, 2L, 1L, 1L, 1L), X5 = c(1L, 3L, 3L, 1L, 2L, 2L, 3L, 
3L, 4L, 1L), X6 = c(2L, 3L, 4L, 4L, 3L, 2L, 4L, 1L, 1L, 3L
), X7 = c(3L, 4L, 1L, 2L, 3L, 3L, 2L, 2L, 2L, 1L), X8 = c(2L, 
4L, 4L, 2L, 3L, 2L, 3L, 4L, 3L, 4L), X9 = c(2L, 1L, 4L, 4L, 
2L, 4L, 4L, 1L, 3L, 3L), X10 = c(3L, 4L, 1L, 3L, 1L, 2L, 
1L, 2L, 2L, 3L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))

# A tibble: 10 x 10
      X1    X2    X3    X4    X5    X6    X7    X8    X9   X10
   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1     4     1     4     1     1     2     3     2     2     3
 2     4     4     3     4     3     3     4     4     1     4
 3     1     4     3     3     3     4     1     4     4     1
 4     4     4     3     2     1     4     2     2     4     3
 5     1     4     2     3     2     3     3     3     2     1
 6     4     2     2     4     2     2     3     2     4     2
 7     2     4     1     2     3     4     2     3     4     1
 8     3     4     1     1     3     1     2     4     1     2
 9     2     3     4     1     4     1     2     3     3     2
10     4     2     2     1     1     3     1     4     3     3

I want to change 1 and 2 to 0, and 3 to 4 to 1.

Expected Output:

# A tibble: 10 x 10
      X1    X2    X3
   <int> <int>   ...
 1     1     0   ... 
 2     1     1   ... 
 3     0     1   ...
 4     1     1   ...
 5     0     1   ...
 6     1     0   ...
 7     0     1   ...
 8     1     1   ...
 9     0     1   ...
10     1     0   ...

I thought I could use dplyr::mutate(), dplyr::across(), dplyr::case_when() but I cannot figure out how.

How can I do this?

CodePudding user response:

Using integer division by 3:

sample_tbl %/% 3

#    X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
# 1   1  0  1  0  0  0  1  0  0   1
# 2   1  1  1  1  1  1  1  1  0   1
# 3   0  1  1  1  1  1  0  1  1   0
# 4   1  1  1  0  0  1  0  0  1   1
# 5   0  1  0  1  0  1  1  1  0   0
# 6   1  0  0  1  0  0  1  0  1   0
# 7   0  1  0  0  1  1  0  1  1   0
# 8   1  1  0  0  1  0  0  1  0   0
# 9   0  1  1  0  1  0  0  1  1   0
# 10  1  0  0  0  0  1  0  1  1   1

CodePudding user response:

We can loop across the column, create a logical vector with %in% on the vector of values 3, 4 and coerce it to binary

library(dplyr)
sample_tbl <- sample_tbl %>% 
       mutate(across(everything(),  ~  (. %in% c(3, 4))))

-output

sample_tbl
# A tibble: 10 x 10
      X1    X2    X3    X4    X5    X6    X7    X8    X9   X10
   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
 1     1     0     1     0     0     0     1     0     0     1
 2     1     1     1     1     1     1     1     1     0     1
 3     0     1     1     1     1     1     0     1     1     0
 4     1     1     1     0     0     1     0     0     1     1
 5     0     1     0     1     0     1     1     1     0     0
 6     1     0     0     1     0     0     1     0     1     0
 7     0     1     0     0     1     1     0     1     1     0
 8     1     1     0     0     1     0     0     1     0     0
 9     0     1     1     0     1     0     0     1     1     0
10     1     0     0     0     0     1     0     1     1     1

If we need to use case_when

sample_tbl <- sample_tbl %>%
      mutate(across(everything(), ~ case_when(. %in% c(3, 4) ~ 1, 
             . %in% c(1, 2) ~ 0)))

Or using base R

sample_tbl[] <- lapply(sample_tbl, \(x)   (x %in% c(3, 4)))

data

sample_tbl <- structure(list(X1 = c(4L, 4L, 1L, 4L, 1L, 4L, 2L, 3L, 2L, 4L), 
    X2 = c(1L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 3L, 2L), X3 = c(4L, 
    3L, 3L, 3L, 2L, 2L, 1L, 1L, 4L, 2L), X4 = c(1L, 4L, 3L, 2L, 
    3L, 4L, 2L, 1L, 1L, 1L), X5 = c(1L, 3L, 3L, 1L, 2L, 2L, 3L, 
    3L, 4L, 1L), X6 = c(2L, 3L, 4L, 4L, 3L, 2L, 4L, 1L, 1L, 3L
    ), X7 = c(3L, 4L, 1L, 2L, 3L, 3L, 2L, 2L, 2L, 1L), X8 = c(2L, 
    4L, 4L, 2L, 3L, 2L, 3L, 4L, 3L, 4L), X9 = c(2L, 1L, 4L, 4L, 
    2L, 4L, 4L, 1L, 3L, 3L), X10 = c(3L, 4L, 1L, 3L, 1L, 2L, 
    1L, 2L, 2L, 3L)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

Here is a base R option.

Change 1 and 2 to 0 and remaining values to 1.

sample_tbl[sample_tbl == 1 | sample_tbl == 2] <- 0
sample_tbl[sample_tbl != 0] <- 1
sample_tbl

#      X1    X2    X3    X4    X5    X6    X7    X8    X9   X10
#   <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
# 1     1     1     1     1     1     0     0     1     1     1
# 2     0     1     0     0     1     1     0     1     1     1
# 3     1     1     0     1     0     1     1     0     1     0
# 4     1     1     1     1     1     1     1     0     0     1
# 5     1     0     1     1     0     1     1     1     0     0
# 6     1     1     0     1     1     1     1     1     0     1
# 7     0     1     1     0     1     1     0     0     0     1
# 8     1     1     0     1     0     0     1     1     0     1
# 9     1     0     1     1     1     1     1     1     1     1
#10     0     1     1     1     0     1     1     0     1     0
  • Related