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

Time:10-08

I tried to run the below code:

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

and expected the following output:

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

However, I am getting the following error:

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

I tried googling the error but couldn't able to understand why it is showing that error. Please, help me in rectifying this error. Thanks in advance.

Data:

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

CodePudding user response:

The quote character may need to be checked. Otherwise, it should work with the reproducible example below

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

The error occurred because of loading extract from another package i.e. from magrittr. It is possible that the OP loaded both packages and the magrittr::extract masked the 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

data

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