0

I am trying to find a quick way to replace the values (strings) in several columns of a dataframe.

id = c(2, 3, 5) 
v1 = c("aa", "bb", "cc") 
v2 = c("ee", "aa", "cc") 
df = data.frame(id,v1,v2) 

print(df)

  id v1 v2
1  2 aa ee
2  3 bb aa
3  5 cc cc

What I want is to replace all instances of "aa" by "uu" and of "cc" by "rr"

  id v1 v2
1  2 uu ee
2  3 bb uu
3  5 rr rr
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Victor A
  • 55
  • 5

7 Answers7

1

Here is a base R option using lapply:

fun <- function(x) {
    x[x == "aa"] <- "uu"
    x[x == "cc"] <- "rr"
    return(x)
}
df <- data.frame(lapply(df, fun))

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can use str_replace_all from the {stringr} package

library(purrr)
library(stringr)
df %>% 
  map_df(~str_replace_all(., c("aa" = "uu", "cc" = "rr"))) 
abichat
  • 2,317
  • 2
  • 21
  • 39
0
library(plyr)
df[c("v1", "v2")] <-lapply(df[c("v1", "v2")], function(x) mapvalues( x, c("aa", "cc"), c("uu", "rr")))
Wyldsoul
  • 1,468
  • 10
  • 16
0

This may work for you:

library(car)
df[,2:3] <- lapply(df[,2:3], function(x) recode(x, "'aa' = 'uu'; 'cc' = 'rr'"))
Seb
  • 5,417
  • 7
  • 31
  • 50
LAP
  • 6,605
  • 2
  • 15
  • 28
  • Thanks it worked perfectly. But what if I want to replace multiple values by one? `df[,2:3] <- lapply(df[,2:3], function(x) recode(x, "'aa' = 'uu'; 'cc' = 'rr';'ee'='rr'"))`. Is there a simple and more efficient way to it? – Victor A Feb 07 '18 at 15:15
  • You can do it like this: `"c('cc', 'ee') = 'rr'"` – LAP Feb 08 '18 at 08:26
0
df %>% mutate_if(is.character, funs(case_when(  . == 'aa' ~ 'uu'
                                              , . == 'cc' ~ 'rr'
                                              , T ~ .)))

If the columns are factors you can do

df %>% mutate_if(is.factor, funs(case_when(   . == 'aa' ~ 'uu'
                                            , . == 'cc' ~ 'rr'
                                            , T ~ as.character(.))))
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
0

First we structure our lookup of replacement values :

lkp = list(aa = 'uu', cc = 'rr')

Then we can use it directly in dplyr::recode, through do.call:

library(dplyr)
df %>%  mutate_at(vars(v1,v2),funs(do.call(recode,c(.x=list(.),lkp))))
#   id v1 v2
# 1  2 uu ee
# 2  3 bb uu
# 3  5 rr rr
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
0

Using the answer from Replace all 0 values to NA , you can subset df using [] to find the values of interest and replace them with their preferred values.

# create data
id <- c(2, 3, 5) 
v1 <- c("aa", "bb", "cc") 
v2 <- c("ee", "aa", "cc") 
df <- data.frame( id, v1, v2, stringsAsFactors = FALSE )

# view the data
df
#   id v1 v2
# 1  2 aa ee
# 2  3 bb aa
# 3  5 cc cc

# find all instances of "aa"
# and replace with "uu"
df[ df == "aa" ] <- "uu"

# find all instances of "cc"
# and replace with "rr"
df[ df == "cc" ] <- "rr"

# view the data
df
#   id v1 v2
# 1  2 uu ee
# 2  3 bb uu
# 3  5 rr rr

# end of script #
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33