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')
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.
Comments