pattern
by replacement
Scan a dataframe and return it with all occurencies of a pattern
changed to replacement
, keeping the same format.
dataset <- data.frame(
col_a = as.factor(letters)[1:7],
col_b = letters[1:7],
col_c = 1:7,
col_d = paste0(letters[1:7], letters[1:7]),
stringsAsFactors = FALSE)
# Show original dataset
dataset
#> col_a col_b col_c col_d
#> 1 a a 1 aa
#> 2 b b 2 bb
#> 3 c c 3 cc
#> 4 d d 4 dd
#> 5 e e 5 ee
#> 6 f f 6 ff
#> 7 g g 7 gg
# replace pattern
replace_pattern(dataset, "a", 'XXX-')
#> col_a col_b col_c col_d
#> 1 XXX- XXX- 1 XXX-XXX-
#> 2 b b 2 bb
#> 3 c c 3 cc
#> 4 d d 4 dd
#> 5 e e 5 ee
#> 6 f f 6 ff
#> 7 g g 7 gg
Exact matching with argument exact
replace_pattern(dataset, "a", 'XXX-', exact = TRUE)
#> col_a col_b col_c col_d
#> 1 XXX- XXX- 1 aa
#> 2 b b 2 bb
#> 3 c c 3 cc
#> 4 d d 4 dd
#> 5 e e 5 ee
#> 6 f f 6 ff
#> 7 g g 7 gg
Test that the levels of a factor vec
are all to be found in the character vector lev
.
## returns TRUE because all levels of iris$species are in c("setosa", "versicolor", "virginica")
is_likert(iris$Species, c("setosa", "versicolor", "virginica"))
#> [1] TRUE
## returns TRUE because all levels of iris$species are in c("setosa", "versicolor", "virginica", "banana"), even though there is actually no level "banana"
# A message is printed
is_likert(iris$Species, c("setosa", "versicolor", "virginica", "banana"))
#> At least one element of lev is not in levels of vec
#> [1] TRUE
## returns FALSE because the "virginica" level of iris$species is missing
is_likert(iris$Species, c("setosa", "versicolor"))
#> [1] FALSE
## returns an error
is_likert(iris$Species, c(1, 2))
#> Error: Elements 1 of is.character(lev) are not true
## returns no error as the numeric is coerced to a character.
is_likert(iris$Species, c("setosa", 2))
#> At least one element of lev is not in levels of vec
#> [1] FALSE
Warnings: is-likert does not test whether the levels of
vec
are a likert scale as in psychometry. See https://en.wikipedia.org/wiki/Likert_scale for example.