2016-11-04 201 views
1

我有一個數據幀,它是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") 

回答

2

一種選擇是從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" 
mgsub