Load packages

library(spHelper)
library(dplyr)

Regular expressions regexp2df

Some simple examples

#>   letter
#> 1      A
#> 2      B
#> 3      C
#> 4      D
#>   word
#> 1    A
#> 2  Bee
#> 3   CE
#> 4    D
#>   Part_A Part_B
#> 1    111    aaa
#> 2    222    bbb
#> 3    333    ccc
#> 4    444    ddd
#> 5    555    eee

Wrong! There must NOT be any SPACES in token’s name:

 patternX <- 'A (?<Part A>.*)  B (?<Part B>.*)'
 regexp2df(text2, patternX)

A more complicated example:

#> sn(?<serial_number>.*) ID_(?<ID>.*)_(?<Class>[NS])(?<Sector>.*)_(?<Point>.*)_[Ss]ubt.*\.
#>   serial_number        ID Class Sector Point
#> 1           555 O20-5-684     N     52     2
#> 2           555 O20-5-984     S     52     8

List all .R files in your working directory:

 regexp2df(dir(),'(?<R_file>.*\\.[rR]$)')
#>                         R_file
#> 1                         <NA>
#> 2       v1_spHelper_Plotting.R
#> 3                         <NA>
#> 4                         <NA>
#> 5          v2_Other_Examples.R
#> 6                         <NA>
#> 7                         <NA>
#> 8 v3_Plotting_Examples_gg_hy.R
#> 9                         <NA>

Do the same by using chaining operator %>%:

 library(dplyr)

 dir() %>% regexp2df('(?<R_file>.*\\.[rR]$)')
#>                         R_file
#> 1                         <NA>
#> 2       v1_spHelper_Plotting.R
#> 3                         <NA>
#> 4                         <NA>
#> 5          v2_Other_Examples.R
#> 6                         <NA>
#> 7                         <NA>
#> 8 v3_Plotting_Examples_gg_hy.R
#> 9                         <NA>

Capture several types of files:

#>                         R_file                       Rmd_file
#> 1                                                            
#> 2       v1_spHelper_Plotting.R                               
#> 3                                    v1_spHelper_Plotting.Rmd
#> 4                                                            
#> 5          v2_Other_Examples.R                               
#> 6                                       v2_Other_Examples.Rmd
#> 7                                                            
#> 8 v3_Plotting_Examples_gg_hy.R                               
#> 9                              v3_Plotting_Examples_gg_hy.Rmd
#>                         HTML_file
#> 1       v1_spHelper_Plotting.html
#> 2                                
#> 3                                
#> 4          v2_Other_Examples.html
#> 5                                
#> 6                                
#> 7 v3_Plotting_Examples_gg_hy.html
#> 8                                
#> 9

which.in family functions

m1 <- matrix(NA, 5, 5)
m1
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   NA   NA   NA   NA   NA
#> [2,]   NA   NA   NA   NA   NA
#> [3,]   NA   NA   NA   NA   NA
#> [4,]   NA   NA   NA   NA   NA
#> [5,]   NA   NA   NA   NA   NA
#> [1]  1  7 13 19 25
#> [1] 1 4
#> [1]  1  7 13 19 25
#>  [1]  2  3  4  5  6  8  9 10 11 12 14 15 16 17 18 20 21 22 23 24
#> [1]  6  7  8  9 10
#> [1]  2  7 12 17 22
#>  [1]  2  3  4  5  8  9 10 14 15 20
#>  [1]  1  2  3  4  5  7  8  9 10 13 14 15 19 20 25
#>  [1]  6 11 12 16 17 18 21 22 23 24
#> [1] TRUE