将提取函数与参数正则表达式一起使用时出错:未使用的参数(正则表达式 =

Error when using extract function with argument regex: unused argument (regex =

我尝试运行下面的代码:

library(tidyverse)
library(dplyr)
library(stringr)
library(tidyr)
tab %>% extract(x, c("feet", "inches"), regex = "(\d)'(\d{1,2})")

并期望得到以下输出:

 #> feet inches  
 #> 1 5 10  
 #> 2 6 1

但是,我收到以下错误:

Error in `[.data.frame`(., x, c("feet", "inches"), regex = "(\d)'(\d{1,2})", :  
 unused argument (regex = "(\d)'(\d{1,2})", remove = FALSE)

我尝试用谷歌搜索错误,但无法理解为什么会显示该错误。请帮我纠正这个错误。提前致谢。

数据:

s <- c("5'10", "6'1")  
tab <- data.frame(x = s)

可能需要检查引号字符。否则,它应该与下面的可重现示例一起使用

library(dplyr)
library(tidyr)
tab %>%
    extract(x, c("feet", "inches"), regex = "(\d)'(\d{1,2})", convert = TRUE)
# A tibble: 2 × 2
   feet inches
  <int>  <int>
1     5     10
2     6      1

错误发生是因为从另一个包加载 extract,即从 magrittr。 OP 可能加载了两个包,并且 magrittr::extract 掩盖了 tidyr::extract

tab %>% 
   magrittr::extract(x, c("feet", "inches"), regex = "(\d)'(\d{1,2})", convert = TRUE)

Error in [.tbl_df(., x, c("feet", "inches"), regex = "(\d)'(\d{1,2})", : object 'x' not found

数据

tab <- tibble(x = c("5'10", "6'1"))