rematch2 icon indicating copy to clipboard operation
rematch2 copied to clipboard

Add tbl_df class to match columns in re_exec() and re_exec_all()

Open gaborcsardi opened this issue 7 years ago • 0 comments

From https://github.com/MangoTheCat/rematch2/issues/9

@krlmlr

Follow-up to https://github.com/MangoTheCat/rematch2/issues/2. This might even avoid the need for a special $ operator, because from there on you have "proper" nested data frames.

pos <- re_exec_all(notables, name_rex)
# pos <- mutate_each(pos, funs(unclass(lapply(., tibble::as.tibble))), first, last)
pos
## # A tibble: 2 × 4
##              first             last
##             <list>           <list>
## 1 <tibble [2 × 3]> <tibble [2 × 3]>
## 2 <tibble [1 × 3]> <tibble [1 × 3]>
## # ... with 2 more variables: .text <chr>, .match <list>
pos$first
## [[1]]
## # A tibble: 2 × 3
##       match start   end
##       <chr> <int> <int>
## 1       Ben     3     5
## 2 Jefferson    20    28
## 
## [[2]]
## # A tibble: 1 × 3
##     match start   end
##     <chr> <int> <int>
## 1 Millard     2     8

@gaborcsardi

Good idea. I don't think the special $ can be avoided, though. You still cannot write pos$first$match...

I would need a hierarchical data frame, not a data frames embedded into cells of data frames.

@krlmlr

No, but now you can do pos %>% unnest(first) %>% .$match . It's not as concise but avoids the magic of a custom class.

@krlmlr

...and the equivalent of pos$first$match would be pos$first %>% lapply("[[", "match"), perhaps there's also a purrr verb for this operation.

Anyway, it's not particularly important, I just thought that returning a nested data frame would be more in line with the "tidy regex" description of the package.

gaborcsardi avatar Jun 21 '17 08:06 gaborcsardi