如何跨多列将 1、2、3、4 更改为 0、1?
How to change 1, 2, 3, 4 to 0, 1 across multiple columns?
有一个小标题包含数字 1 到 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
我想把1和2改成0,3和4改成1
预期输出:
# 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 ...
我以为我可以使用 dplyr::mutate()
、dplyr::across()
、dplyr::case_when()
,但我不知道如何使用。
我该怎么做?
我们可以循环 across
列,在值 3、4 的向量上创建一个带有 %in%
的逻辑向量,并将其强制转换为二进制
library(dplyr)
sample_tbl <- sample_tbl %>%
mutate(across(everything(), ~ +(. %in% c(3, 4))))
-输出
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
如果我们需要使用case_when
sample_tbl <- sample_tbl %>%
mutate(across(everything(), ~ case_when(. %in% c(3, 4) ~ 1,
. %in% c(1, 2) ~ 0)))
或使用base R
sample_tbl[] <- lapply(sample_tbl, \(x) +(x %in% c(3, 4)))
数据
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"))
这是一个基本的 R 选项。
将 1 和 2 更改为 0,将其余值更改为 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
使用整数除以 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
有一个小标题包含数字 1 到 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
我想把1和2改成0,3和4改成1
预期输出:
# 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 ...
我以为我可以使用 dplyr::mutate()
、dplyr::across()
、dplyr::case_when()
,但我不知道如何使用。
我该怎么做?
我们可以循环 across
列,在值 3、4 的向量上创建一个带有 %in%
的逻辑向量,并将其强制转换为二进制
library(dplyr)
sample_tbl <- sample_tbl %>%
mutate(across(everything(), ~ +(. %in% c(3, 4))))
-输出
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
如果我们需要使用case_when
sample_tbl <- sample_tbl %>%
mutate(across(everything(), ~ case_when(. %in% c(3, 4) ~ 1,
. %in% c(1, 2) ~ 0)))
或使用base R
sample_tbl[] <- lapply(sample_tbl, \(x) +(x %in% c(3, 4)))
数据
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"))
这是一个基本的 R 选项。
将 1 和 2 更改为 0,将其余值更改为 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
使用整数除以 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