Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将所有非字母数字替换为句点_R_Gsub_Non Alphanumeric - Fatal编程技术网

R 将所有非字母数字替换为句点

R 将所有非字母数字替换为句点,r,gsub,non-alphanumeric,R,Gsub,Non Alphanumeric,我正试图在一个从政府机构收到的数据框中重命名所有这些糟糕的列名 > colnames(thedata) [1] "Region" "Resource Assessment Site ID" [3] "Site Name/Facility" "Design Head (feet)"

我正试图在一个从政府机构收到的数据框中重命名所有这些糟糕的列名

> 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这里有三个选项需要考虑:

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")

x您需要
[^[:alnum:]+
您可以尝试
make.names(colnames(thedata),allow_=FALSE)
尝试此
模式=“[^a-zA-Z0-9]”
。向正则表达式显示您想要的是
[
]
之间的任意组合,这一点很重要。
^
也必须位于该模式旁边。祝你好运@akrun不错的一个,不知道。或者只是替换“\\W”