Prradep Prradep - 2 months ago 5
R Question

dplyr mutate replace value(s) in a single column based on condition(s) in an efficient way

I would like to replace the values

c t
,
o p
of the column
b
with
c_t
,
o_p
respectively. I have achieved the task using the following approaches.

d <- data.frame(a = c(5,6,3,7,4,3,8,3,2,7),
b = c('c t','c_t','d','o p','o_p','c m','c_t','d','o t','o_p'))
# Way-1
d %>%
mutate(b = replace(b, b == 'c t', 'c_t')) %>%
mutate(b = replace(b, b == 'o p', 'o_p'))

# Way-2
d %>% mutate(b = replace(b, b == 'c t', 'c_t'),
b = replace(b, b == 'o p', 'o_p'))


Output:

# a b
# 1 5 c_t
# 2 6 c_t
# 3 3 d
# 4 7 o_p
# 5 4 o_p
# 6 3 c m
# 7 8 c_t
# 8 3 d
# 9 2 o t
# 10 7 o_p


However, I would like to know if there are any other efficient approaches to achieve this? I would only need to do this for selected values but not all values having space.

Answer Source

We can use sub to match the space (" "), replace with the _ in column 'b'

d %>%
    mutate(b = sub(" ", "_", b))
#   a   b
#1  5 c_t
#2  6 c_t
#3  3   d
#4  7 o_p
#5  4 o_p
#6  3 c_t
#7  8 c_t
#8  3   d
#9  2 o_p
#10 7 o_p

Based on the OP' update,

d %>% 
   mutate(b = as.character(b), 
          b = ifelse(b %in% c('c t', 'o p'), sub(" ", "_", b), b) )