针对 r 中的特定条件将字符重新编码为数字
recode character to numeric for specific conditions in r
我有一个看起来像这样的原始数据集。:
> df.1
id score
1 13_B 1
2 13_C 4
3 133_D 5
4 141 2
5 145 3
6 143 4
7 12_B 6
8 12_C 7
9 12_D 9
我需要做一些需要所有 id
数字的过程,因此我将 _B|_C|_D
重新编码为 1|2|3
。
在我对具有额外 group
列的数据集完成一些处理后,我的示例数据集如下所示:
df.2 <- data.frame(id = c("131","132","133", "141", "145", "143", "121","122","123"),
score = c(1,4,5,2,3,4,6,7,9),
group = c(5,5,5,4,4,4,3,3,3))
> df.2
id score group
1 131 1 5
2 132 4 5
3 133 5 5
4 141 2 4
5 145 3 4
6 143 4 4
7 121 6 3
8 122 7 3
9 123 9 3
此时,我需要将 ids
转换回那些 items = c(12,13,15)
的原始值。所以 15
不在这个数据集中,但需要在全球范围内工作的东西。我想要的输出是:
> df.3
id score group
1 13_B 1 5
2 13_C 4 5
3 13_D 5 5
4 141 2 4
5 145 3 4
6 143 4 4
7 12_B 6 3
8 12_C 7 3
9 12_D 9 3
有什么想法吗?
谢谢!
使用 str_replace_all
通过传递命名向量 (setNames
) 重新编码子字符串替换
library(dplyr)
library(stringr)
df.1 %>%
mutate(id1 = as.numeric(str_replace_all(str_replace(id, "^(\d{2})\d+_(.*)",
"\1_\2"), setNames(as.character(c(1, 2, 3)), c("_B", "_C", "_D")))))
-输出
id score id1
1 13_B 1 131
2 13_C 4 132
3 133_D 5 133
4 141 2 141
5 145 3 145
6 143 4 143
7 12_B 6 121
8 12_C 7 122
9 12_D 9 123
用于从 'df.2'
替换
df.2 %>%
mutate(id2 = case_when(substr(id, 1, 2) %in% c(12, 13, 15) ~
str_replace_all(as.character(id), setNames(c("_B", "_C", "_D"),
str_c(1:3, "$"))), TRUE ~as.character(id)))
-输出
id score group id2
1 131 1 5 13_B
2 132 4 5 13_C
3 133 5 5 13_D
4 141 2 4 141
5 145 3 4 145
6 143 4 4 143
7 121 6 3 12_B
8 122 7 3 12_C
9 123 9 3 12_D
数据
df.1 <- structure(list(id = c("13_B", "13_C", "133_D", "141", "145",
"143", "12_B", "12_C", "12_D"), score = c(1L, 4L, 5L, 2L, 3L,
4L, 6L, 7L, 9L)), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9"), class = "data.frame")
你可以试试这个:
df.2 %>%
group_by(group) %>%
mutate(group_id=row_number(),
x= paste0("_", LETTERS[2:4])) %>%
mutate(id2 = ifelse(!str_detect(id,"14"), paste0(str_sub(id,1,2),x),id)) %>%
select(id, id2, score, group)
id id2 score group
<chr> <chr> <dbl> <dbl>
1 131 13_B 1 5
2 132 13_C 4 5
3 133 13_D 5 5
4 141 141 2 4
5 145 145 3 4
6 143 143 4 4
7 121 12_B 6 3
8 122 12_C 7 3
9 123 12_D 9 3
我有一个看起来像这样的原始数据集。:
> df.1
id score
1 13_B 1
2 13_C 4
3 133_D 5
4 141 2
5 145 3
6 143 4
7 12_B 6
8 12_C 7
9 12_D 9
我需要做一些需要所有 id
数字的过程,因此我将 _B|_C|_D
重新编码为 1|2|3
。
在我对具有额外 group
列的数据集完成一些处理后,我的示例数据集如下所示:
df.2 <- data.frame(id = c("131","132","133", "141", "145", "143", "121","122","123"),
score = c(1,4,5,2,3,4,6,7,9),
group = c(5,5,5,4,4,4,3,3,3))
> df.2
id score group
1 131 1 5
2 132 4 5
3 133 5 5
4 141 2 4
5 145 3 4
6 143 4 4
7 121 6 3
8 122 7 3
9 123 9 3
此时,我需要将 ids
转换回那些 items = c(12,13,15)
的原始值。所以 15
不在这个数据集中,但需要在全球范围内工作的东西。我想要的输出是:
> df.3
id score group
1 13_B 1 5
2 13_C 4 5
3 13_D 5 5
4 141 2 4
5 145 3 4
6 143 4 4
7 12_B 6 3
8 12_C 7 3
9 12_D 9 3
有什么想法吗?
谢谢!
使用 str_replace_all
通过传递命名向量 (setNames
) 重新编码子字符串替换
library(dplyr)
library(stringr)
df.1 %>%
mutate(id1 = as.numeric(str_replace_all(str_replace(id, "^(\d{2})\d+_(.*)",
"\1_\2"), setNames(as.character(c(1, 2, 3)), c("_B", "_C", "_D")))))
-输出
id score id1
1 13_B 1 131
2 13_C 4 132
3 133_D 5 133
4 141 2 141
5 145 3 145
6 143 4 143
7 12_B 6 121
8 12_C 7 122
9 12_D 9 123
用于从 'df.2'
替换df.2 %>%
mutate(id2 = case_when(substr(id, 1, 2) %in% c(12, 13, 15) ~
str_replace_all(as.character(id), setNames(c("_B", "_C", "_D"),
str_c(1:3, "$"))), TRUE ~as.character(id)))
-输出
id score group id2
1 131 1 5 13_B
2 132 4 5 13_C
3 133 5 5 13_D
4 141 2 4 141
5 145 3 4 145
6 143 4 4 143
7 121 6 3 12_B
8 122 7 3 12_C
9 123 9 3 12_D
数据
df.1 <- structure(list(id = c("13_B", "13_C", "133_D", "141", "145",
"143", "12_B", "12_C", "12_D"), score = c(1L, 4L, 5L, 2L, 3L,
4L, 6L, 7L, 9L)), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9"), class = "data.frame")
你可以试试这个:
df.2 %>%
group_by(group) %>%
mutate(group_id=row_number(),
x= paste0("_", LETTERS[2:4])) %>%
mutate(id2 = ifelse(!str_detect(id,"14"), paste0(str_sub(id,1,2),x),id)) %>%
select(id, id2, score, group)
id id2 score group
<chr> <chr> <dbl> <dbl>
1 131 13_B 1 5
2 132 13_C 4 5
3 133 13_D 5 5
4 141 141 2 4
5 145 145 3 4
6 143 143 4 4
7 121 12_B 6 3
8 122 12_C 7 3
9 123 12_D 9 3