#install_github("zhamel/foo")
library(foo)
This package consists of 4 functions which hopefully can make your life easier when dealing with factors.
fbind()
Goal: Bind two factors together to get back a factor with the levels from both factors.
fbind(iris$Species[c(1, 51, 101)], PlantGrowth$group[c(1, 11, 21)])
#> [1] setosa versicolor virginica ctrl trt1 trt2
#> Levels: ctrl setosa trt1 trt2 versicolor virginica
freq_out()
Goal: Create a data frame consisting of the levels of the factor and its frequency.
freq_out(iris$Species)
#> Source: local data frame [3 x 2]
#>
#> x n
#> (fctr) (int)
#> 1 setosa 50
#> 2 versicolor 50
#> 3 virginica 50
reorder_levels()
Goal: Reordering the levels based on frequency (from least to most)
reorder_levels(factor(c("a","a","b","c","c","c","c","d","d","d")))
#> [1] a b c d
#> Levels: c d a b
factor_to_char()
Goal: Should your factor be a character instead? This function compares the length of the factor with the number of levels. If the length of the factor = # of levels of the factor, then factor_to_char()
returns TRUE
: maybe this shouldn't be a factor! Otherwise, if they are different, factor_to_char()
returns FALSE
: this time you may want to keep it as a factor!
factor_to_char(iris$Species)
#> [1] FALSE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.