str_split在字符向量中的不同位置处第一次和第二次出现分隔符

时间:2018-06-15 13:01:37

标签: r regex stringr

我有一个字符列表,其中包含天气变量,后跟" mean _#"其中#是5到10之间的数字。我想将列表子集化为仅具有天气变量名称。平均天气变量如下所示:

> mean_vars
 [1] "dew_mean_10"        "dew_mean_5"         "dew_mean_6"         "dew_mean_7"        
 [5] "dew_mean_8"         "dew_mean_9"         "humid_mean_10"      "humid_mean_5"      
 [9] "humid_mean_6"       "humid_mean_7"       "humid_mean_8"       "humid_mean_9"      
[13] "rain_mean_10"       "rain_mean_5"        "rain_mean_6"        "rain_mean_7"       
[17] "rain_mean_8"        "rain_mean_9"        "soil_moist_mean_10" "soil_moist_mean_5" 
[21] "soil_moist_mean_6"  "soil_moist_mean_7"  "soil_moist_mean_8"  "soil_moist_mean_9" 
[25] "soil_temp_mean_10"  "soil_temp_mean_5"   "soil_temp_mean_6"   "soil_temp_mean_7"  
[29] "soil_temp_mean_8"   "soil_temp_mean_9"   "solar_mean_10"      "solar_mean_5"      
[33] "solar_mean_6"       "solar_mean_7"       "solar_mean_8"       "solar_mean_9"      
[37] "temp_mean_10"       "temp_mean_5"        "temp_mean_6"        "temp_mean_7"       
[41] "temp_mean_8"        "temp_mean_9"        "wind_dir_mean_10"   "wind_dir_mean_5"   
[45] "wind_dir_mean_6"    "wind_dir_mean_7"    "wind_dir_mean_8"    "wind_dir_mean_9"   
[49] "wind_gust_mean_10"  "wind_gust_mean_5"   "wind_gust_mean_6"   "wind_gust_mean_7"  
[53] "wind_gust_mean_8"   "wind_gust_mean_9"   "wind_spd_mean_10"   "wind_spd_mean_5"   
[57] "wind_spd_mean_6"    "wind_spd_mean_7"    "wind_spd_mean_8"    "wind_spd_mean_9"

这就是我想要的结果:

> var_names                                                                                           
       "dew"      "humid"       "rain"      "solar"       "temp" "soil_moist"  "soil_temp"    "wind_dir"  "wind_gust"   "wind_spd" 

现在我想出了如何做到这一点,但由于缺乏正则表达式的能力,我填写我的方法是无关紧要的。我也将不得不重复我的过程20次代替"意思是"换句话说。

var_names <- unique(str_split_fixed(mean_vars, "_", n = 3)[c(1:18,31:42),1])
var_names <- unlist(c(var_names, unique(unite(as_tibble(str_split_fixed(mean_vars, "_", n = 3)[c(19:30,43:60), 1:2])))))

我一直试图尽可能地保持在tidyverse软件包的范围内,所以我使用了stringr :: str_split_fixed。

如果你有一个使用同样功能的解决方案,那将是理想的,因为我可以继续相同的编程风格,但我对所有建议持开放态度。

感谢。

1 个答案:

答案 0 :(得分:1)

使用subunique。这个更短,没有包依赖(或者使用unique(str_replace(mean_vars, "_mean.*", ""))和stringr):

unique(sub("_mean.*", "", mean_vars))

,并提供:

 [1] "dew"        "humid"      "rain"       "soil_moist" "soil_temp" 
 [6] "solar"      "temp"       "wind_dir"   "wind_gust"  "wind_spd"  

如果由于某种原因你真的想使用str_split,那么:

rmMean <- function(x) paste(head(x, -2), collapse = "_")
unique(sapply(str_split(mean_vars, "_"), rmMean))

注意

mean_vars <- c("dew_mean_10", "dew_mean_5", "dew_mean_6", "dew_mean_7", "dew_mean_8", 
"dew_mean_9", "humid_mean_10", "humid_mean_5", "humid_mean_6", 
"humid_mean_7", "humid_mean_8", "humid_mean_9", "rain_mean_10", 
"rain_mean_5", "rain_mean_6", "rain_mean_7", "rain_mean_8", "rain_mean_9", 
"soil_moist_mean_10", "soil_moist_mean_5", "soil_moist_mean_6", 
"soil_moist_mean_7", "soil_moist_mean_8", "soil_moist_mean_9", 
"soil_temp_mean_10", "soil_temp_mean_5", "soil_temp_mean_6", 
"soil_temp_mean_7", "soil_temp_mean_8", "soil_temp_mean_9", "solar_mean_10", 
"solar_mean_5", "solar_mean_6", "solar_mean_7", "solar_mean_8", 
"solar_mean_9", "temp_mean_10", "temp_mean_5", "temp_mean_6", 
"temp_mean_7", "temp_mean_8", "temp_mean_9", "wind_dir_mean_10", 
"wind_dir_mean_5", "wind_dir_mean_6", "wind_dir_mean_7", "wind_dir_mean_8", 
"wind_dir_mean_9", "wind_gust_mean_10", "wind_gust_mean_5", "wind_gust_mean_6", 
"wind_gust_mean_7", "wind_gust_mean_8", "wind_gust_mean_9", "wind_spd_mean_10", 
"wind_spd_mean_5", "wind_spd_mean_6", "wind_spd_mean_7", "wind_spd_mean_8", 
"wind_spd_mean_9")