【问题标题】:R extract function gives a weird error about the regexR extract 函数给出了一个关于正则表达式的奇怪错误
【发布时间】:2021-11-07 03:36:12
【问题描述】:

我有一个包含一列的 excel 文件。此列有一个数字和与此数字对应的状态。

State
01 Alabama
02 Alaska

等等。我想将此列提取为两列,一列包含数字,另一列包含州名。我尝试使用 tidyr 中的 extract():

df <- read_xlsx("States.xlsx")
df %>% tidyr::extract(States,c("A","B"),sep="(\\d\\d) ([a-zA-Z]+)")

但是,它会吐出错误:

Error: `regex` should define 2 groups;  found.
Error: 1 components of `...` were not used.

We detected these problematic arguments:
* `sep`

Did you misspecify an argument?

任何想法我做错了什么?谢谢!

【问题讨论】:

    标签: r dplyr split extract tidyr


    【解决方案1】:

    根据?extract

    regex - 用于提取所需值的正则表达式。 into 的每个元素都应该有一个组(由 () 定义)。

    extract 中没有名称为 sep 的参数。它是regexsepseparate 中的一个参数,而不是 extract 中的一个参数

    library(dplyr)
    df %>% 
      tidyr::extract(States,c("A","B"),regex="(\\d\\d) ([a-zA-Z]+)")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多