I have a tibble, e.g.
a <- as_tibble(c("201.1, 202 (abc) 203, 204", "301 / 302.22 def, 303"))
value
<chr>
1 201.1, 202 (abc) 203, 204
2 301 / 302.22 def, 303
Now I would like to get a data.frame with two columns
[1,] 201.1 202
[2,] 301 302.22
by cutting everything after the second number (202 in the first row, 302.22 in the second row) and separating the remining part of the expression with delimiter "," or "/" to get the two columns.
Here are several approaches.
1) separate Use
separatefrom tidyr like this giving the tibble/data.frame shown. It automatically determines that the columns are numeric.2) Base R Use
strcapturefrom base R like this. No packages are needed.3) read.pattern Use
read.patternfrom gsubfn. This uses the same regex as in (2). It automatically determines that the columns are numeric and uses the sametext=andcol.names=arguments asread.tablemaking them easy to remember if you are familiar with that.Note
The input from the question