速度变量/数据列命名规范:m/s、>=、m/s²等特殊符号处理咨询
列名规范重命名替换方案
核心特殊符号映射规则
- 速度单位
m/s:替换为m_per_s,避免与其他含s的标识混淆,语义明确 - 大于等于符号
>=:替换为通用缩写ge(对应英文greater than or equal,行业通用识别度高) - 符号
-分场景处理:- 位于数值前作为负数标识:替换为
neg - 位于两个数值之间作为范围连接符:替换为
to
- 位于数值前作为负数标识:替换为
- 加速度单位
m/s²:替换为m_per_s_sq,sq为平方(square)的通用缩写
通用辅助处理规则
- 所有空格、括号、特殊非语义符号统一替换为下划线
_ - 连续出现的多个下划线合并为单个下划线
- 最终输出的列名首尾不留下划线
示例转换效果
对应提供的6个示例列名,转换后结果如下:
- 原:
"SpeedZone 3m/s (Distance) (m)"→ 转换后:SpeedZone_3m_per_s_Distance_m - 原:
"SpeedZone >=3m/s (Distance) (m)"→ 转换后:SpeedZone_ge_3m_per_s_Distance_m - 原:
"SpeedBand1 Band1 (1.0-3.0 m/s)(Distance) (m)"→ 转换后:SpeedBand1_Band1_1.0_to_3.0_m_per_s_Distance_m - 原:
"SpeedBand1 >= Band1 (1.0-3.0 m/s) (Distance) (m)"→ 转换后:SpeedBand1_ge_Band1_1.0_to_3.0_m_per_s_Distance_m - 原:
"Decel Zone -6 to -5m/s²"→ 转换后:Decel_Zone_neg6_to_neg5_m_per_s_sq - 原:
"Accel Zone 5-6m/s²"→ 转换后:Accel_Zone_5_to_6_m_per_s_sq
参考R实现代码
library(stringr) standardize_colname <- function(col) { # 优先替换长字符特殊单位 col <- str_replace_all(col, "m/s²", "m_per_s_sq") col <- str_replace_all(col, "m/s", "m_per_s") # 替换比较符号 col <- str_replace_all(col, ">=", "ge") # 替换数值前的负号 col <- str_replace_all(col, "(?<=\\D)-(?=\\d)", "neg") # 替换数值之间的范围连接符 col <- str_replace_all(col, "(?<=\\d)-(?=\\d)", "_to_") # 所有非字母、数字、点的字符替换为下划线 col <- str_replace_all(col, "[^a-zA-Z0-9.]", "_") # 合并连续下划线、去除首尾下划线 col <- str_replace_all(col, "_+", "_") col <- str_remove_all(col, "^_|_$") return(col) } # 测试 column_names <- c("SpeedZone 3m/s (Distance) (m)", "SpeedZone >=3m/s (Distance) (m)", "SpeedBand1 Band1 (1.0-3.0 m/s)(Distance) (m)", "SpeedBand1 >= Band1 (1.0-3.0 m/s) (Distance) (m)", "Decel Zone -6 to -5m/s²", "Accel Zone 5-6m/s²") standardize_colname(column_names)
内容的提问来源于stack exchange,提问作者seansteele
相关产品推荐
相关产品推荐

