knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

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 very useful 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.

Installation

devtools::install_github("qiaoyuet/foofactors")

Quick demo

fbind()

Binding two factors via fbind():

library(foofactors)
a <- factor(c("character", "hits", "your", "eyeballs"))
b <- factor(c("but", "integer", "where it", "counts"))

Simply catenating two factors leads to a result that most don't expect.

c(a, b)

The fbind() function glues two factors together and returns factor.

fbind(a, b)

freq_out()

Often we want a table of frequencies for the levels of a factor. The base table() function returns an object of class table, which can be inconvenient for downstream work. Processing with as.data.frame() can be helpful but it's a bit clunky.

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

The freq_out() function returns a frequency table as a well-named tbl_df:

freq_out(x)

detect_fct()

R is not so good as detecting whether a factor is actually just characters. We consider a factor is a true factor when the number of unique values in it does not equal to the length of it. For example, this is a factor with two levels:

a <- factor(c("A","A","B"))
length(unique(a))
length(a)
detect_fct(a)

If the number of unique values does equal to the length of the factor we consider it as characters instead of a factor. For example, this should be characters instead of a factor with three levels:

b <- factor(c("A","B","C"))
length(unique(b))
length(b)
detect_fct(b)

reorder_desc()

This function can reorder the levels of a factor into descending order. The descending levels of strings are automatically considered as descending alphabetical order in R.

a <- factor(c("1","3","2","6"))
levels(a)
levels(reorder_desc(a))

b <- factor(c("Statistics","Mathematics","Computer Science"))
levels(b)
levels(reorder_desc(b))

factor.asis()

This function allows you to set levels as the unique values that appear in the data. For example, we want the levels to be "high","low","medium" instead of "high","low","low","high","medium".

a <- factor(c("high","low","low","high","medium"))
factor_asis(a)


qiaoyuet/foofactors documentation built on May 29, 2019, 12:06 p.m.