r - what happened when `separate_wider_regex` in multiple match - Stack Overflow

admin2025-04-22  1

I tested the following script, but it displays an error, what happened?

I jut want save Y & 33 & N & A (Y33@N(A)) into four separate columns.

library('tidyverse')

tta <- data.frame(res=c("Y33@N(A)","H5@O(B)"))
ttb <- tta %>%
    separate_wider_regex(res, patterns=c(resn="^[A-Za-z]+",
                                         resi="\\d+(?=@)",
                                         name="(?<=\\@)[a-zA-Z]+(?=\\()",
                                         chain="(?<=\\()[a-zA-Z]+(?=\\))"
                                         ),
                         too_few="debug")

I tested the following script, but it displays an error, what happened?

I jut want save Y & 33 & N & A (Y33@N(A)) into four separate columns.

library('tidyverse')

tta <- data.frame(res=c("Y33@N(A)","H5@O(B)"))
ttb <- tta %>%
    separate_wider_regex(res, patterns=c(resn="^[A-Za-z]+",
                                         resi="\\d+(?=@)",
                                         name="(?<=\\@)[a-zA-Z]+(?=\\()",
                                         chain="(?<=\\()[a-zA-Z]+(?=\\))"
                                         ),
                         too_few="debug")
Share Improve this question asked Jan 21 at 15:15 cwindcwind 4323 silver badges9 bronze badges 1
  • 1 Who can say! What was the error? I get only a warning... – Limey Commented Jan 21 at 15:20
Add a comment  | 

1 Answer 1

Reset to default 3

Motivated by "Unnamed components will match, but not be included in the output" in the online doc description of the patterns parameter, this

ttb <- tta %>%
  separate_wider_regex(res, patterns=c(resn="^[A-Za-z]+",
                                       resi="\\d+",
                                       "\\@",
                                       name="[a-zA-Z]+",
                                       "\\(",
                                       chain="[a-zA-Z]+",
                                       "\\)$"
  ))

appears to give you what you want for your test data, though I suspect your use case may require more complicated regexes.

ttb
# A tibble: 2 × 4
  resn  resi  name  chain
  <chr> <chr> <chr> <chr>
1 Y     33    N     A    
2 H     5     O     B    
转载请注明原文地址:http://anycun.com/QandA/1745301465a90582.html