我想替换一列中的多个值。假设您在数据框中有一个名为“var1”的列。
testing <- data.frame(var1 = c(LETTERS[1:5],
'Payments12',
'Balance',
'Default',
'Currentterm',
'Interest',
'Original.Valuation1',
'REV_Capped',
'Amount',
'NoofHoliday'))
我想用右手边替换左手边。如果没有找到任何值,它应该保持不变(按原样)。
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')
由于问题是在 dplyr 中标记的,因此您可以使用 dplyr::mutate 和 dplyr::recode 来解决此类问题。如果问题更复杂(例如有条件),您可以使用 dplyr::case_when
在上面的例子中,代码是这样的。只有给定的重新编码值将被更改。
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'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句