Skip to contents

Substitute node columns (both columns 1 and 2) of a graph data frame with a dictionary of substitutions.

Usage

graph_subs(DF, df_subs = NA, rm_symbols = "[^\\w\\s\\_\\&]")

Arguments

DF

a graph data frame

df_subs

a data frame with the substitutions to be made, where the first column is what to substitute and column 2 is what to substitute with. It uses regular expressions. It can be left empty to use only the rm_symbols parameter

rm_symbols

remove non-word characters (symbols), preserving letter, numbers and the underscore (to make it clear the words boundaries). Default regex pattern: "[^\w\s\_\&]". Is possible to use other custom regex patterns. If no symbol substitution is desired, leave it empty..

Examples

# a test tibble
test_graph <- tibble::tibble(
  n1 = c("A", "B", "A", "C", "B", "Ab", "A", "D"),
  n2 = c("B", "Ab", "B", "D", "C", "A", "C", "D")
)

# dataframe with substitutions
DF_substitution <- tibble::tribble(
  ~col1, ~col2,
  "B", "blah",
  "C", "Capybara"
)

# Doing the substitutions
graph_subs(test_graph, DF_substitution)
#> # A tibble: 8 × 2
#>   n1       n2      
#>   <chr>    <chr>   
#> 1 A        blah    
#> 2 blah     Ab      
#> 3 A        blah    
#> 4 Capybara D       
#> 5 blah     Capybara
#> 6 Ab       A       
#> 7 A        Capybara
#> 8 D        D