This is the README for thefoofactors
package, which is a toy package created for STAT547. It is not recommended for general use, so if you want to know more about dealing withfactor
, you can downloadforcats
package.
Thefoofactors
package contains 7 functions to help users taking control of factors easier and less annoying.
devtools::install_github("Tangjiahui26/foofactors")
fbind()
fbind()
combines two factors in a way that is more logical than c(a,b). The return value offbind()
is also a factor.
library(foofactors)
a <- factor(c("character", "hits", "your", "eyeballs"))
b <- factor(c("but", "integer", "where it", "counts"))
c(a,b)
#> [1] 1 3 4 2 1 3 4 2
fbind(a, b)
#> [1] character hits your eyeballs but integer where it
#> [8] counts
#> Levels: but character counts eyeballs hits integer where it your
freq_out()
set.seed(1234)
x <- factor(sample(letters[1:5], size = 100, replace = TRUE))
Thefreq_out()
function returns a frequency table as a well-namedtbl_df
:
freq_out(x)
#> # A tibble: 5 x 2
#> x n
#> <fctr> <int>
#> 1 a 25
#> 2 b 26
#> 3 c 17
#> 4 d 17
#> 5 e 15
detect_factors()
The function detect_factors()
is used to check if some of the element of the factor are not unique. Otherwise, if all of the elements are unique, you probably should use it as character.
a <- factor(c("a","b","c")) ##Should be character
b <- factor(c("a","b","b")) ##Should be factor
detect_factors(a)
#> [1] FALSE
detect_factors(b)
#> [1] TRUE
check_factors()
If you want to check a dataframe for factors, you can usecheck_factors()
. This functions will check if your dataframe contains any factors you don't want to use.
check_factors(iris)
#> [1] "Species"
check_factors(mtcars)
#> character(0)
new_factor()
and new_factor_rev()
The function new_factor()
would like to set your factors to the order in which they appear in the data. In contrast, If you want the reverse order,new_factor_rev()
can be used in your codes.
a <- factor(c("c","b","a"))
b <- factor(c("banana","apple","pear"))
levels(a)
#> [1] "a" "b" "c"
new_factor(a)
#> [1] c b a
#> Levels: c b a
levels(b)
#> [1] "apple" "banana" "pear"
new_factor(b)
#> [1] banana apple pear
#> Levels: banana apple pear
Examples fornew_factor_rev()
:
new_factor_rev(a)
#> [1] c b a
#> Levels: a b c
new_factor_rev(b)
#> [1] banana apple pear
#> Levels: pear apple banana
new_reorder()
Sometimes we may want to change the order of factors, and instead order them in descending way. The functionnew_reorder()
can help you to realize it.
a <- factor(c("a","b","c"))
levels(a)
#> [1] "a" "b" "c"
levels(new_reorder(a))
#> [1] "c" "b" "a"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.