spHelper
examplesv2_Other_Examples.Rmd
Load packages
regexp2df
Some simple examples
text1 <- c("_A","_Bee","_CE","_D")
pattern1A <- '_(?<letter>.)' # captures only the first symbol
regexp2df(text1, pattern1A)
#> letter
#> 1 A
#> 2 B
#> 3 C
#> 4 D
#> word
#> 1 A
#> 2 Bee
#> 3 CE
#> 4 D
text2 <- c("A_111 B_aaa",
"A_222 B_bbb",
"A_333 B_ccc",
"A_444 B_ddd",
"A_555 B_eee")
pattern2 <- 'A_(?<Part_A>.*) B_(?<Part_B>.*)'
regexp2df(text2, pattern2)
#> 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:
text3 <- c("sn555 ID_O20-5-684_N52_2_Subt2_01.",
"sn555 ID_O20-5-984_S52_8_Subt10_11.")
pattern3 <- paste0('sn(?<serial_number>.*) ',
'ID_(?<ID>.*)',
'_(?<Class>[NS])',
'(?<Sector>.*)',
'_(?<Point>.*)',
'_[Ss]ubt.*\\.');
cat(pattern3)
#> 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:
#> 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 %>%
:
#> 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:
expr <- paste0('(?<R_file>.*\\.[rR]$)|',
'(?<Rmd_file>.*\\.[rR]md$)|',
'(?<HTML_file>.*\\.html$)')
dir() %>% regexp2df(expr)
#> 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#> [,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
# ================================
r1 <- which.in(trilow, m1)
r2 <- which.in.trilow(m1)
identical(r1, r2)
#> [1] TRUE
corr_vec2mat
# ------------------------------------------------------------
# Example 1A: Vector into a matrix
vector = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
corr_vec2mat(vector)
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0 0.1 0.2 0.3
#> [2,] 0.1 1.0 0.4 0.5
#> [3,] 0.2 0.4 1.0 0.6
#> [4,] 0.3 0.5 0.6 1.0
# ------------------------------------------------------------
# Example 1B: Vector is transformed to a matrix by filling
# it column-wise:
# In this example only the matrix elements of interest are shown""
vector = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
matrix =
[,1] [,2] [,3] [,4]
[1,] . . . .
[2,] 0.1 . . .
[3,] 0.2 0.4 . .
[4,] 0.3 0.5 0.6 .
# Find similarity between 1A and 1B examples.
# ---------------------------------------------------------------------
# Example 2: inappropriate number of coefficients - warning appears
# corr_vec2mat(vector[1:5])
#> Warning message:
#> Only first 3 coefficient(s) out of 5 will be used to construct symmetric matrix with 3 rows.
# --------------------------------------------------------------------
# Example 3: ERROR appears - values of coeffs must be between [-1;1]
# corr_vec2mat(1:5)
#> Error in corr_vec2mat(1:5) :
#> All values in input vector must be between [-1 and 1]
This chapter is out of date and must be updated!!!
# Data
df <- mtcars[,c("cyl","gear")]
# Function, that uses `getVarValues`:
f1 <- function(data, v1) { getVarValues(v1, data) }
# Returns values of `df$cyl`:
f1(df, cyl)
f1(df, "cyl")
cyl <- "gear" # !!! Still values of `df$cyl`, not `df$gear`:
f1(df, cyl)
# Returns values of `df$gear`:
f1(df, gear)
# Returns values of vector `a`, as there is no variable `df$a`:
a = "cyl"
f1(df, a)
var <- c("My", "variable", "var")
f1(df, var)
# A Data frame
df <- data.frame(A = "Values_A_(DATA.FRAME)",
E = "Values_E_(DATA.FRAME)", stringsAsFactors = FALSE)
# Vectors
A <- "Values of the vector 'A'"
B <- "Values of the vector 'B'"
# A call object `CALL`:
fun <- function(data, gr, ID) { match.call() }
CALL <- fun(df, A, B)
CALL
# Outputs of `getVarValues` -------------------------------------------------
getVarValues(VAR = gr, DATA = df, CALL = CALL)
getVarValues(gr, df, CALL)
getVarValues(A, df, CALL)
getVarValues(B, df, CALL)
getVarValues(ID, df, CALL) # `ID` found only in function's `fun` definition.
# `df$ID` does not exist.
getVarValues(F, df, CALL) # `F` is a special variable: `F = FALSE`
getVarValues(c, df, CALL) # `c()` is a function.
getVarValues(G, df, CALL) # ERROR, as neither variable `G` nor `df$G` exists.