Replace multiple values in r

john

I want to replace multiple values in a column. Suppose you have a column called 'var1' in dataframe.

testing <- data.frame(var1 = c(LETTERS[1:5], 
                               'Payments12',
                               'Balance',
                               'Default',
                               'Currentterm',
                               'Interest',
                               'Original.Valuation1',
                               'REV_Capped',
                               'Amount',
                               'NoofHoliday'))

I want to replace Left hand side with Right hand side. If any value is not found, it should be unchanged (as it is).

c('Payments12' = 'No. of Payments in 12 Months')
c('Balance' = 'Current Balance Bands')
c('Default' = 'Default (>=3 Months)')
c('Currentterm' = 'Current Term')
c('Interest' = 'Interest Rate')
c('Original.Valuation1' = 'Original Valuation')
c('REV_Capped' = 'REV Capped')
c('Amount' = 'Payment received in 12 Months')
c('NoofHoliday' = 'No of Months Holiday')
Arnaud Perigord

As the question is tagged in dplyr, you can use dplyr::mutate and dplyr::recode for this kind of question. If the problem is more complex (with conditions for example) you can use dplyr::case_when

In the exemple above, the code would be like this. Only given recode values will be changed.

library(dplyr)
testing <- data.frame(var1 = c(LETTERS[1:5], 
                               'Payments12',
                               'Balance',
                               'Default',
                               'Currentterm',
                               'Interest',
                               'Original.Valuation1',
                               'REV_Capped',
                               'Amount',
                               'NoofHoliday')) %>%
  mutate(var1 = recode(var1, 
                'Payments12' = 'No. of Payments in 12 Months', 
                'Balance' = 'Current Balance Bands',
                'Default' = 'Default (>=3 Months)',
                'Currentterm' = 'Current Term',
                'Interest' = 'Interest Rate',
                'Original.Valuation1' = 'Original Valuation',
                'REV_Capped' = 'REV Capped',
                'Amount' = 'Payment received in 12 Months',
                'NoofHoliday' = 'No of Months Holiday'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace multiple values in a list in R

R: Replace multiple values in multiple columns of dataframes with values in another column

Replace row values with multiple conditions in r

Substituting multiple values in R using Sub or replace

find and replace values in multiple columns in R

Replace values across multiple varibles in R

R: replace a value of a vector with multiple values

how to replace values in multiple rows in a dataframe in r?

R loop replace multiple values in matrix

replace multiple values in data.table with R

Replace multiple similar values in a column in R

Replace multiple values over multiple columns of a data frame in R

R: Replace multiple values in multiple columns of dataframes with NA

replace multiple values with PySpark

pandas replace multiple values

Replace multiple values in a column

MySQL REPLACE multiple values

Replace multiple values in a matrix

replace multiple values in a string

Replace multiple values - MySQL

Replace specific chr values within groups for multiple variables in R

Replace multiple "less than values" in different columns in R

R: Smart way to replace column/vector values by multiple rules?

R data.table replace values in multiple columns

replace values with NA across multiple columns if a condition is met in R

Compare and replace values among multiple columns using a vectorized code in R

R replace_na values conditionally by column with multiple conditions

Easiest way to replace values in multiple columns at once in R

Replace values in a vector in R