Introduction

Factors are a very useful type of variable in R, but they can also drive you nuts. Especially the "stealth factor" that you think of as character.

The goal of foofactor is to practice and develop an R package. The foofactors package has five functions:

Installation

# install.packages("devtools")
library(devtools)
library(foofactors)

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. 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: reorder_new

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. and 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.