Multi replace values according to template

We can stack the list to a key/value dataset ('df2') and then do a match between the 'coli' of 'df' with 'values' column of 'df2' to get the corresponding index for 'ind' and assign it to 'newi'

df2 <- stack(replaceList)
df$newi <- df2$ind[match(df$coli, df2$values)]
df
#   coli         newi
#1     4 threeAndFour
#2     3 threeAndFour
#3     6   fiveAndSix
#4     1    oneAndTwo
#5     2    oneAndTwo
#6     1    oneAndTwo
#7     5   fiveAndSix
#8     2    oneAndTwo
#9     4 threeAndFour
#10    6   fiveAndSix
#11    3 threeAndFour
#12    5   fiveAndSix

Make a named vector instead of your replaceList list, then match by name:

set.seed(1337);df <- data.frame(coli = sample(rep(1:6,2)), newi = 0 )

# make a named vector
myLookup <- setNames(c("oneAndTwo","oneAndTwo","threeAndFour","threeAndFour","fiveAndSix","fiveAndSix"),
                   1:6)

# then match by name
df$newi <- myLookup[ df$coli ]

# check
head(df)
#   coli         newi
# 1    1    oneAndTwo
# 2    6   fiveAndSix
# 3    1    oneAndTwo
# 4    5   fiveAndSix
# 5    3 threeAndFour
# 6    2    oneAndTwo

Other (preferred) option would be to use cut, and get factor column:

# using cut, no need for lookup
df$newiFactor <- cut(df$coli, c(0, 2, 4, 6))

# check
head(df[order(df$coli), ])
#    coli         newi newiFactor
# 1     1    oneAndTwo      (0,2]
# 3     1    oneAndTwo      (0,2]
# 6     2    oneAndTwo      (0,2]
# 8     2    oneAndTwo      (0,2]
# 5     3 threeAndFour      (2,4]
# 11    3 threeAndFour      (2,4]

Note: we could use labels option for cut and get your desired naming "oneAndTwo", etc. Again, in this case, I prefer to have numerical looking names: "(0,2]", etc.

Tags:

R