NOTE: This is a toy package created for expository purposes. It is not meant to actually be useful. If you want a package for factor handling, please see forcats.

foofactors

Factors are a handy type of variable in R, but they can also drive you nuts. This package provides some helper functions for the care and feeding of factors.

Description: This is a package, which we can find some new function such as fbind(),order_appeared(), reorder_new(),etc. Factors have driven people to extreme measures, like ordering custom conference ribbons and laptop stickers to express how HELLNO we feel about stringsAsFactors. And yet, sometimes you need them. Can they be made less maddening? Let's find out. This package is similar to forcats package, and you could find more factor function in forcats.

Rscripts for functions are in R folder

Tests for functions are in the test folder

Installation

# install.packages("devtools")
library(devtools)
devtools::install_github("yanchaoluo/foofactors")
library(foofactors)

The foofactors package has five functions:

Binding two factors using fbind():

The fbind function puts two factors together as one.

factor1<- factor(c("This", "is", "test", "for"))
factor2 <- factor(c("how", "to", "use", "fbind","function"))

If we only use c function, which we can not get the expected result.

c(factor1, factor2)

Using fbind() function.

fbind(factor1, factor2)

Make a frequency table for a factor: freq_out()

The freq_out gives a dataframe with factors and their frequency.

If we could use as.data.frame function.

set.seed(1)
sample_letters <- factor(sample(letters[1:4], size = 100, replace = TRUE))
as.data.frame(table(sample_letters))

The freq_out() function is much easy to make the frequency table.

freq_out(sample_letters)

Detect factors that should be character: detect_factor_be_character

It's hard to figure out factor and character in R, for example:

factor1<- class(factor(c("apple", "pear", "banana")))
factor2<- class(factor(c("apple", "pear", "apple")))

From above code, both "factor1" and "factor2" are defined as the factor by using class(). However, "factor1" should be a character because of the number of unique value equal to the length.

Using detect_factor_be_character function can easily find "character", "factor" and neither "character" or "factor."

factor1 <- factor(c("apple", "pear", "banana"))
detect_factor_be_character(factor1)
factor2 <-  factor(c("apple", "pear", "apple"))
factor3<- c(1,2,2,3)
detect_factor_be_character(factor2)
detect_factor_be_character(factor3) 

The R code above will show two types of error when you use "detect_factor_be_character."

Reorder level of factor:

This function is very similar with fct_rev function. and reorder_new fuction is using desc() in dplyr to reorder factor levels in descending order.

reorder_new(factor(c("1","2","3")))

From the code above, we could find that the orginal order levels is 1, 2, 3. Using the reorder_new function we get decending order 3, 2, 1.

Sets levels to the order in which they appear in the data: order_appeared

Here I create a function which set the order following appearance level. This function will only return unique input levels,

factor(c("pear","apple","banana","banana","apple"))
order_appeared (c("pear","apple","banana","banana","apple"))

If we only use factorfunction, the factor level will follow the alphabet. Using order_appeared function, the factor level will order in which they appear in the data.



yanchaoluo/foofactors documentation built on May 18, 2019, 9:15 p.m.