【发布时间】:2020-04-30 17:38:18
【问题描述】:
我正在尝试在 R 中使用 dplyr 在以下示例中由变量 name 的某些实例过滤的数据帧中的变量字符串之后提取子字符串。我试图将所需的结果传递给一个名为income_rent 的新变量。
我是正则表达式的新手。我的尝试是:
income_cashrent <- v18 %>%
filter(str_detect(name, "B25122")) %>%
mutate(income_rent = str_extract(label, "[^--!!]*$"))
但是,我得到了结果:
Error in stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) : Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)
name的前四行是:
Estimate!!Total
Estimate!!Total!!Household income in the past 12 months (in 2018 inflation-adjusted dollars) --!!Less than $10,000
Estimate!!Total!!Household income in the past 12 months (in 2018 inflation-adjusted dollars) --!!Less than $10,000!!With cash rent
Estimate!!Total!!Household income in the past 12 months (in 2018 inflation-adjusted dollars) --!!Less than $10,000!!With cash rent!!Less than $100
期望的结果是:
[not sure how to indicate an empty result here]
Less than $10,000
Less than $10,000!!With cash rent
Less than $10,000!!With cash rent!!Less than $100
到目前为止,我一直无法调试这个,参考堆栈上的其他正则表达式示例。任何指导都将受到欢迎。提前谢谢大家!
【问题讨论】: