我有一个数据框,在“角色”列中,每一行的值均为“学生”或“老师”。我想将这些值分别替换为“ st”或“ te”。
roles_complete <- c("students","teacher")
roles_standard <- c("st", "te")
data$roles <- stri_replace_all(data$roles, regex= roles_complete, roles_standard)
很奇怪,这只会更改某些值。
roles
1 st
2 students
3 teacher
4 te
5 st
6 students
7 teacher
8 te
9 st
10 students
11 teacher
12 te
我显然已对插入stri_replace的条件做错了,但文档不是很清楚。
使用功能str_replace_all()
,您可以在命名向量中准确定义如何替换匹配的模式:
library(stringr)
data <- c("students", "teacher", "students", "teacher")
str_replace_all(data, c("students" = "st", "teacher" = "te"))
因此,您的情况:
data$roles <- str_replace_all(data$roles, c("students" = "st", "teacher" = "te"))
通常,R将字符向量解释为因素。如果您的数据集中是这种情况:
data <- data.frame(roles = sample(c("students","teacher"), 12, replace = TRUE))
levels(data$roles)
# [1] "students" "teacher"
levels(data$roles) <- c("st", "te")
levels(data$roles)
# [1] "st" "te"
您可以在recode()
中使用dplyr
。
library(dplyr)
df %>%
mutate(roles = recode(roles, "students" = "st", "teacher" = "te"))
或
roles_complete <- c("students", "teacher")
roles_standard <- c("st", "te")
df %>%
mutate(roles = recode(roles, !!!setNames(roles_standard, roles_complete)))
基础R解决方案
df$role <- factor(df$role, labels = c("st", "te"))
您可以尝试
data$roles <- replace(data$roles,roles_complete,roles_standard)