Suppose I have a dataset:
Have:
example1 example2
11-2001-6 st3829s
11-2001-6 s8290s
11-201-6 sts39
Want:
example1 example2
2001 3829
2001 8290
NA NA
I want to output the numbers that are 4 contiguous numbers or n numbers (specify length). If no group of 4 numbers occurs together return NA.
CodePudding user response:
First Things we are talking about regular expression. Second i will solve this using PHP language and you should find the solution for your program language
preg_match_all('/[0-9][0-9][0-9][0-9]/', '11-2001-6 st3829s', $output_array);
this will output
array(1
0 => array(2
0 => 2001
1 => 3829
)
)
but if you path
preg_match_all('/[0-9][0-9][0-9][0-9]/', '11-201-6 sts39', $output_array);
this will output
array(1
0 => array()
)
so you have check the output and if it empty array you have return NA
CodePudding user response:
We may use str_extract
and mutate_all
library(stringr)
library(dplyr)
mutate_all(df, ~str_extract(., pattern= "\\d{4}"))
example1 example2
1 2001 3829
2 2001 8290
3 <NA> <NA>