This package makes it easy to identify objects or object elements which store a simpler hidden type. "Hidden type" refers to a simpler type or class that we could convert an object to without losing information.
A common example is numerics stored in characters.
Additionally the functions unfactor
and make_logical
allow for the quick conversions that base R doesn't.
Let's get those out of the way first and then go through the hides_*
functions.
library(hidden)
Convert a factor or the factors of a data.frame, list or environment to the type they hide (character or numeric).
make_logical(c(0,1,2,1,2,0), na.values = 0)
#> 1 is set to TRUE, 2 is set to FALSE
#> [1] NA TRUE FALSE TRUE FALSE NA
## it's not safe not to mention the true and false values, here yes becomes FALSE
make_logical(c("yes","no","yes","maybe"), na.values = "maybe")
#> no is set to TRUE, yes is set to FALSE
#> [1] FALSE TRUE FALSE NA
## we can mention only one of the true/false values
make_logical(c("yes","no","yes","maybe"), false = "no",na.values = "maybe")
#> yes is set to TRUE, is set to FALSE
#> [1] TRUE FALSE TRUE NA
## no message is displayed if both true/false values are given
make_logical(c("yes","no","yes","maybe"), true = "yes", false = "no",na.values = "maybe")
#> [1] TRUE FALSE TRUE NA
Convert to logical if hides_lgl(x, ...)
is TRUE
.
unfactor(factor(c(1,2,3)))
#> [1] 1 2 3
unfactor(factor(c("a","b","c")))
#> [1] "a" "b" "c"
iris2 <- iris
iris2$Sepal.Width <- factor(iris2$Sepal.Width)
str(iris2)
#> 'data.frame': 150 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : Factor w/ 23 levels "2","2.2","2.3",..: 15 10 12 11 16 19 14 14 9 11 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
str(unfactor(iris))
#> 'data.frame': 150 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : chr "setosa" "setosa" "setosa" "setosa" ...
TRUE
if character or factor could be converted to numeric without loss of information.
## hides_num
hides_num(c("1","2",NA))
#> [1] TRUE
hides_num(c("1.5","2",NA))
#> [1] TRUE
hides_num(factor(c(1,2,NA)))
#> [1] TRUE
hides_num(c(1, 2, NA))
#> [1] FALSE
hides_num(c(1.5, 2, NA))
#> [1] FALSE
hides_num(c(1L, 2L, NA))
#> [1] FALSE
hides_num(c(1, 2, NA), obvious = TRUE)
#> [1] TRUE
hides_num(c(1.5 , 2, NA),obvious = TRUE)
#> [1] TRUE
hides_num(c(1L,2L,NA), obvious = TRUE)
#> [1] TRUE
TRUE
if numeric, character or factor could be converted to integer without loss of information.
## hides_int
hides_int(c("1","2",NA))
#> [1] TRUE
hides_int(c("1","2","a",NA),"a")
#> [1] TRUE
hides_int(c("1.5","2",NA))
#> [1] FALSE
hides_int(factor(c(1,2,NA)))
#> [1] TRUE
hides_int(c(1, 2, NA))
#> [1] TRUE
hides_int(c(1.5, 2, NA))
#> [1] FALSE
hides_int(c(1L, 2L, NA))
#> [1] FALSE
hides_int(c(1, 2, NA), obvious = TRUE)
#> [1] TRUE
hides_int(c(1.5 , 2, NA),obvious = TRUE)
#> [1] FALSE
hides_int(c(1L,2L,NA), obvious = TRUE)
#> [1] TRUE
TRUE
if numeric, character or factor contains binary values that could be converted to without loss of information other than labels (see make_logical
).
## hides_lgl
hides_lgl(c("yes","no","yes",NA))
#> [1] TRUE
hides_lgl(c("yes","no","yes","maybe"))
#> [1] FALSE
hides_lgl(c("yes","no","yes","maybe"),"maybe")
#> [1] TRUE
hides_lgl(factor(c("yes","no","yes",NA)))
#> [1] TRUE
hides_lgl(factor(c("yes","no","yes","maybe")))
#> [1] FALSE
hides_lgl(factor(c("yes","no","yes","maybe")),"maybe")
#> [1] TRUE
hides_lgl(c("1","2",NA))
#> [1] TRUE
hides_lgl(c("1.5","2",NA))
#> [1] TRUE
hides_lgl(factor(c(1,2,NA)))
#> [1] TRUE
hides_lgl(c(1, 2, NA))
#> [1] TRUE
hides_lgl(c(1.5, 2, NA))
#> [1] TRUE
hides_lgl(c(1L, 2L, NA))
#> [1] TRUE
hides_lgl(c(TRUE, FALSE, NA))
#> [1] FALSE
hides_lgl(c(TRUE, FALSE, NA), obvious = TRUE)
#> [1] TRUE
TRUE
if data.frame list or environment contains only objects of length 1.
## hides_vec
hides_vec(list("a","b","c"))
#> [1] TRUE
hides_vec(list(1,2,3))
#> [1] TRUE
hides_vec(list("a",2,3L))
#> [1] FALSE
hides_vec(list(c("a","z"),"b","c"))
#> [1] FALSE
hides_vec(c("a","b","c"))
#> [1] FALSE
hides_vec(c("a","b","c"), obvious = TRUE)
#> [1] TRUE
TRUE
if list or environment contains only objects of same length.
hides_df(list(1:5,6:10))
#> [1] TRUE
hides_df(list(1:5,6:11))
#> [1] FALSE
hides_df(list(letters[1:5],6:10))
#> [1] TRUE
hides_df(list(letters[1:5],letters[6:10]))
#> [1] TRUE
hides_df(iris)
#> [1] FALSE
hides_df(as.list(iris))
#> [1] TRUE
hides_df(iris, obvious = TRUE)
#> [1] TRUE
TRUE
if data frame, list or environment contains only objects of same length and same type.
## hides_mat
hides_mat(list(1:5,6:10))
#> [1] TRUE
hides_mat(list(1:5,6:11))
#> [1] FALSE
hides_mat(list(letters[1:5],6:10))
#> [1] FALSE
hides_mat(list(letters[1:5],letters[6:10]))
#> [1] TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.