将 1 列中的多个值替换为 R 中的单个值
Replacing multiple values in 1 column to a single value in R
我有一个名为 data
的数据框。其中一列是 data$activity
.
data$activity = c("eat", "sing", "dance", "sing", "eat and shop", "shop", "drinks",...)
我希望将 data$activity
列中的特定值替换为“Companionship”(字符串),这些特定值存储在以下向量中:
leisure = c("eat", "drinks", "shop", "eat and shop")
我尝试了以下方法,
data$activity[data$activity== leisure] <- "Companionship"
#to replace values in the 'leisure' vector that are found in data$activity, with the string, 'Companionship'
但错误是:Warning in data$activity == leisure : longer object length is not a multiple of shorter object length
使用基础 R,我们可以创建要替换的搜索词(即 "eat|drinks|shop|eat and shop"
)。 |
表示“或”,因此我们将查找 eat 或 drinks 或...等。如果我们找到这些术语,那么我们将用 Companionship
.
替换它们
data$activity <- gsub(paste(leisure, collapse = "|"), "Companionship", data$activity)
activity
1 Companionship
2 sing
3 dance
4 sing
5 Companionship
6 Companionship
7 Companionship
或 tidyverse
:
library(tidyverse)
data %>%
mutate(activity = str_replace_all(activity, paste(leisure, collapse = "|", "$", sep = ""), "Companionship"))
或者按照您的方法,您只需要使用 %in%
而不是 ==
,因为您想要匹配 leisure
.
中的任何值
data$activity[data$activity %in% leisure] <- "Companionship"
这对我有用:
library(stringr)
data = data.frame(activity = c("eat", "sing", "dance", "sing", "eat and shop", "shop", "drinks"))
leisure = c("eat", "drinks", "shop", "eat and shop")
data[str_detect(data$activity, pattern = paste0(leisure,collapse = "|")) , "activity"] <- "Companionship"
data
# activity
# 1 Companionship
# 2 sing
# 3 dance
# 4 sing
# 5 Companionship
# 6 Companionship
# 7 Companionship
我有一个名为 data
的数据框。其中一列是 data$activity
.
data$activity = c("eat", "sing", "dance", "sing", "eat and shop", "shop", "drinks",...)
我希望将 data$activity
列中的特定值替换为“Companionship”(字符串),这些特定值存储在以下向量中:
leisure = c("eat", "drinks", "shop", "eat and shop")
我尝试了以下方法,
data$activity[data$activity== leisure] <- "Companionship"
#to replace values in the 'leisure' vector that are found in data$activity, with the string, 'Companionship'
但错误是:Warning in data$activity == leisure : longer object length is not a multiple of shorter object length
使用基础 R,我们可以创建要替换的搜索词(即 "eat|drinks|shop|eat and shop"
)。 |
表示“或”,因此我们将查找 eat 或 drinks 或...等。如果我们找到这些术语,那么我们将用 Companionship
.
data$activity <- gsub(paste(leisure, collapse = "|"), "Companionship", data$activity)
activity
1 Companionship
2 sing
3 dance
4 sing
5 Companionship
6 Companionship
7 Companionship
或 tidyverse
:
library(tidyverse)
data %>%
mutate(activity = str_replace_all(activity, paste(leisure, collapse = "|", "$", sep = ""), "Companionship"))
或者按照您的方法,您只需要使用 %in%
而不是 ==
,因为您想要匹配 leisure
.
data$activity[data$activity %in% leisure] <- "Companionship"
这对我有用:
library(stringr)
data = data.frame(activity = c("eat", "sing", "dance", "sing", "eat and shop", "shop", "drinks"))
leisure = c("eat", "drinks", "shop", "eat and shop")
data[str_detect(data$activity, pattern = paste0(leisure,collapse = "|")) , "activity"] <- "Companionship"
data
# activity
# 1 Companionship
# 2 sing
# 3 dance
# 4 sing
# 5 Companionship
# 6 Companionship
# 7 Companionship