Home > Back-end >  Replacing multiple values in 1 column to a single value in R
Replacing multiple values in 1 column to a single value in R

Time:02-28

I have a dataframe called data. One of its columns is data$activity.

data$activity = c("eat", "sing", "dance", "sing", "eat and shop", "shop", "drinks",...)

I wish to replace specific values within the data$activity column to "Companionship" (string), and these specific values are stored in the following vector:

leisure = c("eat", "drinks", "shop", "eat and shop")

I tried the following,

data$activity[data$activity== leisure] <- "Companionship"

#to replace values in the 'leisure' vector that are found in data$activity, with the string, 'Companionship'

But error is: Warning in data$activity == leisure : longer object length is not a multiple of shorter object length

CodePudding user response:

With base R, we can create search terms that we want to replace (i.e., "eat|drinks|shop|eat and shop"). The | means "or", so we will look for eat or drinks or ... etc. If we find those terms, then we will replace them with 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

Or with tidyverse:

library(tidyverse)

data %>% 
  mutate(activity = str_replace_all(activity, paste(leisure, collapse = "|"), "Companionship"))

Or with your approach, you just need to use %in% rather than ==, as you want to match to any value in leisure.

data$activity[data$activity %in% leisure] <- "Companionship"

CodePudding user response:

This is working to me:

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
  • Related