2017-04-18 44 views
3

我試圖在從政府機構收到的數據框中重命名所有這些殘酷的列名。用句號替換所有非字母數字

> colnames(thedata) 
[1] "Region"          "Resource Assessment Site ID"     
[3] "Site Name/Facility"       "Design Head (feet)"       
[5] "Design Flow (cfs)"       "Installed Capacity (kW)"      
[7] "Annual Production (MWh)"      "Plant Factor"        
[9] "Total Construction Cost (1,000 $)"   "Annual O&M Cost (1,000 $)"     
[11] "Cost per Installed Capacity ($/kW)"   "Benefit Cost Ratio with Green Incentives" 
[13] "IRR with Green Incentives"     "Benefit Cost Ratio without Green Incentives" 
[15] "IRR without Green Incentives" 

列標題包含特殊的非字母數字字符和空格,因此不可能引用它們,因此我必須重命名它們。我想用句號替換所有非字母數字字符。但我想:

old.col.names <- colnames(thedata) 
new.col.names <- gsub("^a-z0-9", ".", old.col.names) 

的^是一個「不」的劃分,所以我認爲這將取代一切,是不是字母與在old.col.names一個時期。

任何人都可以幫忙嗎?

+2

你需要'[^ [:alnum:] ] +' – akrun

+2

你可以嘗試'make.names(colnames(thedata),allow_ = FALSE)' – Jimbou

+1

試試'pattern =「[^ a-zA-Z0-9]」'。顯示正則表達式是非常重要的,你需要'['和']'之間的任何組合。 '^'也必須在該模式旁邊。祝你好運! –

回答

0

這裏有三個問題需要考慮:

make.names(x) 
gsub("[^A-Za-z0-9]", ".", x) 
names(janitor::clean_names(setNames(data.frame(matrix(NA, ncol = length(x))), x))) 

這裏的每一個樣子:

make.names(x) 
## [1] "Region"          "Resource.Assessment.Site.ID"     
## [3] "Site.Name.Facility"       "Design.Head..feet."       
## [5] "Design.Flow..cfs."       "Installed.Capacity..kW."      
## [7] "Annual.Production..MWh."      "Plant.Factor"        
## [9] "Total.Construction.Cost..1.000..."   "Annual.O.M.Cost..1.000..."     
## [11] "Cost.per.Installed.Capacity....kW."   "Benefit.Cost.Ratio.with.Green.Incentives" 
## [13] "IRR.with.Green.Incentives"     "Benefit.Cost.Ratio.without.Green.Incentives" 
## [15] "IRR.without.Green.Incentives"    

gsub("[^A-Za-z0-9]", ".", x) 
## [1] "Region"          "Resource.Assessment.Site.ID"     
## [3] "Site.Name.Facility"       "Design.Head..feet."       
## [5] "Design.Flow..cfs."       "Installed.Capacity..kW."      
## [7] "Annual.Production..MWh."      "Plant.Factor"        
## [9] "Total.Construction.Cost..1.000..."   "Annual.O.M.Cost..1.000..."     
## [11] "Cost.per.Installed.Capacity....kW."   "Benefit.Cost.Ratio.with.Green.Incentives" 
## [13] "IRR.with.Green.Incentives"     "Benefit.Cost.Ratio.without.Green.Incentives" 
## [15] "IRR.without.Green.Incentives"    

library(janitor) 
names(clean_names(setNames(data.frame(matrix(NA, ncol = length(x))), x))) 
## [1] "region"          "resource_assessment_site_id"     
## [3] "site_name_facility"       "design_head_feet"       
## [5] "design_flow_cfs"        "installed_capacity_kw"      
## [7] "annual_production_mwh"      "plant_factor"        
## [9] "total_construction_cost_1_000"    "annual_o_m_cost_1_000"      
## [11] "cost_per_installed_capacity_kw"    "benefit_cost_ratio_with_green_incentives" 
## [13] "irr_with_green_incentives"     "benefit_cost_ratio_without_green_incentives" 
## [15] "irr_without_green_incentives"    

的樣本數據:

x <- c("Region", "Resource Assessment Site ID", "Site Name/Facility", 
    "Design Head (feet)", "Design Flow (cfs)", "Installed Capacity (kW)", 
    "Annual Production (MWh)", "Plant Factor", "Total Construction Cost (1,000 $)", 
    "Annual O&M Cost (1,000 $)", "Cost per Installed Capacity ($/kW)", 
    "Benefit Cost Ratio with Green Incentives", "IRR with Green Incentives", 
    "Benefit Cost Ratio without Green Incentives", "IRR without Green Incentives") 
相關問題