自动从名称更改名称到标签

时间:2016-11-04 20:17:46

标签: r

我的数据框是df的子集,我想交换标签的名称 完整列表有255行,我总是有一个可变列数的子集。如何自动执行此操作以便自动进行交换?我可以使用向量和矩阵来解决此问题而不是数据帧。我解决了,但我的方法很笨重。我正在寻找一种更好的方法来进行这种类型的名称值交换。

label <- c("Current Loan Payment", "Current Loan Payment Amount","Current Loan Payment Due Date","Fixed/Manual Loan Payment Amount","Is Manual Amortization Schedule","Re-amortize as Balloon Payment")

name <- c("Credit__Current_Loan_Payment__c" , "Credit__Current_Loan_Payment_Amount__c", "Credit__Current_Loan_Payment_Due_Date__c","Credit__Manual_Loan_Payment_Amount__c","Credit__Is_Manual_Amortization_Schedule__c","Credit__Re_amortize_as_Balloon_Payment__c" )

df <- data.frame(label, name)

df.namesToSwap <- c("Credit__Current_Loan_Payment__c", "Credit__Is_Manual_Amortization_Schedule__c" )

df.labelsIWant <- c("Current Loan Payment", "Is Manual Amortization Schedule")

1 个答案:

答案 0 :(得分:2)

一个选项是来自mgsub

qdap
library(qdap)
df$name <- mgsub(df.namesToSwap, df.labelsIWant, df$name)
df$name
#[1] "Current Loan Payment"                      "Credit__Current_Loan_Payment_Amount__c"    "Credit__Current_Loan_Payment_Due_Date__c" 
#[4] "Credit__Manual_Loan_Payment_Amount__c"     "Is Manual Amortization Schedule"           "Credit__Re_amortize_as_Balloon_Payment__c"