在 R 中使用 stringr 提取变量名
Extract variable names using stringr in R
我正在尝试从以下向量中提取一些变量名称和数字并将它们存储到两个新变量中:
unique_strings <- c("PM_1_PMS5003_S_Avg", "PM_2_5_PMS5003_S_Avg", "PM_10_PMS5003_S_Avg",
"PM_1_PMS5003_A_Avg", "PM_2_5_PMS5003_A_Avg", "PM_10_PMS5003_A_Avg",
"PNC_0_3_PMS5003_Avg", "PNC_0_5_PMS5003_Avg", "PNC_1_0_PMS5003_Avg",
"PNC_2_5_PMS5003_Avg", "PNC_5_0_PMS5003_Avg", "PNC_10_0_PMS5003_Avg",
"PM_1_PMS7003_S_Avg", "PM_2_5_PMS7003_S_Avg", "PM_10_PMS7003_S_Avg",
"PM_1_PMS7003_A_Avg", "PM_2_5_PMS7003_A_Avg", "PM_10_PMS7003_A_Avg",
"PNC_0_3_PMS7003_Avg", "PNC_0_5_PMS7003_Avg", "PNC_1_0_PMS7003_Avg",
"PNC_2_5_PMS7003_Avg", "PNC_5_0_PMS7003_Avg", "PNC_10_0_PMS7003_Avg"
)
我想提取第一个变量 PMS
之前的每个字符。这包括带有 PM
或 PNC
的字符串,以及下划线和数字。我想将这些结果存储到一个名为 pollutant
的变量中。
期望的输出:
unique(pollutant)
[1] "PM_1" "PM_2_5" "PM_10" "PNC_0_3" "PNC_0_5" "PNC_1_0" "PNC_2_5" "PNC_5_0" "PNC_10"
我想提取第二个变量 PMS
之后的所有内容。
为此,我首先尝试从每个字符串中仅提取型号(以 003
结尾的四位数),但是,包含 A_Avg
或 S_Avg
也在提取中。
这是我的第一次尝试:
model_id <- str_extract(unique_strings, "[0-9]{4,}")
unique(model_id)
[1] "5003" "7003"
我以前没有使用过正则表达式,并且很难浏览现有的文档/堆栈帖子。感谢您的意见!
我们可以使用 str_extract
从字符串 (^(PM|PNC)
) 的开头 (^
) 开始匹配 'PM' 或 'PNC',后跟一个_
和一个或多个数字 (\d+
) 后跟具有另一组 _
和数字的案例(为此我们指定零个或多个 ((_\d)*
)
library(stringr)
out <- str_extract(unique_strings, "^(PM|PNC)_\d+(_\d)*")
这将为那些没有匹配项的元素提供 NA
。如果我们需要删除那些
na.omit(out)
对于第二种情况,不清楚想要的输出。如果我们需要提取 PMS
之后的所有内容,我们可以使用正则表达式查找 ((?<=PMS)
) 并匹配 (.*
)
之后的所有字符
str_extract(unique_strings, "(?<=PMS).*")
我们可以用str_split
在"PMS"
的基础上拆分字符串。之后,使用 str_replace
删除第一列中的最后一个 "_"
。输出为 m
。第一个变量在第一列,第二个变量在第二列。
library(stringr)
m <- str_split(unique_strings, pattern = "PMS", simplify = TRUE)
m[, 1] <- str_replace(m[, 1], "_$", "")
m
# [,1] [,2]
# [1,] "PM_1" "5003_S_Avg"
# [2,] "PM_2_5" "5003_S_Avg"
# [3,] "PM_10" "5003_S_Avg"
# [4,] "PM_1" "5003_A_Avg"
# [5,] "PM_2_5" "5003_A_Avg"
# [6,] "PM_10" "5003_A_Avg"
# [7,] "PNC_0_3" "5003_Avg"
# [8,] "PNC_0_5" "5003_Avg"
# [9,] "PNC_1_0" "5003_Avg"
# [10,] "PNC_2_5" "5003_Avg"
# [11,] "PNC_5_0" "5003_Avg"
# [12,] "PNC_10_0" "5003_Avg"
# [13,] "PM_1" "7003_S_Avg"
# [14,] "PM_2_5" "7003_S_Avg"
# [15,] "PM_10" "7003_S_Avg"
# [16,] "PM_1" "7003_A_Avg"
# [17,] "PM_2_5" "7003_A_Avg"
# [18,] "PM_10" "7003_A_Avg"
# [19,] "PNC_0_3" "7003_Avg"
# [20,] "PNC_0_5" "7003_Avg"
# [21,] "PNC_1_0" "7003_Avg"
# [22,] "PNC_2_5" "7003_Avg"
# [23,] "PNC_5_0" "7003_Avg"
# [24,] "PNC_10_0" "7003_Avg"
我正在尝试从以下向量中提取一些变量名称和数字并将它们存储到两个新变量中:
unique_strings <- c("PM_1_PMS5003_S_Avg", "PM_2_5_PMS5003_S_Avg", "PM_10_PMS5003_S_Avg",
"PM_1_PMS5003_A_Avg", "PM_2_5_PMS5003_A_Avg", "PM_10_PMS5003_A_Avg",
"PNC_0_3_PMS5003_Avg", "PNC_0_5_PMS5003_Avg", "PNC_1_0_PMS5003_Avg",
"PNC_2_5_PMS5003_Avg", "PNC_5_0_PMS5003_Avg", "PNC_10_0_PMS5003_Avg",
"PM_1_PMS7003_S_Avg", "PM_2_5_PMS7003_S_Avg", "PM_10_PMS7003_S_Avg",
"PM_1_PMS7003_A_Avg", "PM_2_5_PMS7003_A_Avg", "PM_10_PMS7003_A_Avg",
"PNC_0_3_PMS7003_Avg", "PNC_0_5_PMS7003_Avg", "PNC_1_0_PMS7003_Avg",
"PNC_2_5_PMS7003_Avg", "PNC_5_0_PMS7003_Avg", "PNC_10_0_PMS7003_Avg"
)
我想提取第一个变量 PMS
之前的每个字符。这包括带有 PM
或 PNC
的字符串,以及下划线和数字。我想将这些结果存储到一个名为 pollutant
的变量中。
期望的输出:
unique(pollutant)
[1] "PM_1" "PM_2_5" "PM_10" "PNC_0_3" "PNC_0_5" "PNC_1_0" "PNC_2_5" "PNC_5_0" "PNC_10"
我想提取第二个变量 PMS
之后的所有内容。
为此,我首先尝试从每个字符串中仅提取型号(以 003
结尾的四位数),但是,包含 A_Avg
或 S_Avg
也在提取中。
这是我的第一次尝试:
model_id <- str_extract(unique_strings, "[0-9]{4,}")
unique(model_id)
[1] "5003" "7003"
我以前没有使用过正则表达式,并且很难浏览现有的文档/堆栈帖子。感谢您的意见!
我们可以使用 str_extract
从字符串 (^(PM|PNC)
) 的开头 (^
) 开始匹配 'PM' 或 'PNC',后跟一个_
和一个或多个数字 (\d+
) 后跟具有另一组 _
和数字的案例(为此我们指定零个或多个 ((_\d)*
)
library(stringr)
out <- str_extract(unique_strings, "^(PM|PNC)_\d+(_\d)*")
这将为那些没有匹配项的元素提供 NA
。如果我们需要删除那些
na.omit(out)
对于第二种情况,不清楚想要的输出。如果我们需要提取 PMS
之后的所有内容,我们可以使用正则表达式查找 ((?<=PMS)
) 并匹配 (.*
)
str_extract(unique_strings, "(?<=PMS).*")
我们可以用str_split
在"PMS"
的基础上拆分字符串。之后,使用 str_replace
删除第一列中的最后一个 "_"
。输出为 m
。第一个变量在第一列,第二个变量在第二列。
library(stringr)
m <- str_split(unique_strings, pattern = "PMS", simplify = TRUE)
m[, 1] <- str_replace(m[, 1], "_$", "")
m
# [,1] [,2]
# [1,] "PM_1" "5003_S_Avg"
# [2,] "PM_2_5" "5003_S_Avg"
# [3,] "PM_10" "5003_S_Avg"
# [4,] "PM_1" "5003_A_Avg"
# [5,] "PM_2_5" "5003_A_Avg"
# [6,] "PM_10" "5003_A_Avg"
# [7,] "PNC_0_3" "5003_Avg"
# [8,] "PNC_0_5" "5003_Avg"
# [9,] "PNC_1_0" "5003_Avg"
# [10,] "PNC_2_5" "5003_Avg"
# [11,] "PNC_5_0" "5003_Avg"
# [12,] "PNC_10_0" "5003_Avg"
# [13,] "PM_1" "7003_S_Avg"
# [14,] "PM_2_5" "7003_S_Avg"
# [15,] "PM_10" "7003_S_Avg"
# [16,] "PM_1" "7003_A_Avg"
# [17,] "PM_2_5" "7003_A_Avg"
# [18,] "PM_10" "7003_A_Avg"
# [19,] "PNC_0_3" "7003_Avg"
# [20,] "PNC_0_5" "7003_Avg"
# [21,] "PNC_1_0" "7003_Avg"
# [22,] "PNC_2_5" "7003_Avg"
# [23,] "PNC_5_0" "7003_Avg"
# [24,] "PNC_10_0" "7003_Avg"