knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
This is a package built for STAT545 homework08. It contains several functions mainly used to deal with factors. Here is an example of one of the function fbind
library(MaryFactors) fbind(iris$Species[c(1, 51, 101)], PlantGrowth$group[c(1, 11, 21)])
fbind
This function conbines levels of two vectors. Numeric vectors will be treated as factors
fbind(iris$Species[c(1, 51, 101)], PlantGrowth$group[c(1, 11, 21)]) a <- c(1, 2) b <- c('a', 'b') fbind(a, b) #Cannot accept weird input b <- as.data.frame(b) fbind(a, b)
freq_out
This function makes a frequency table for a factor.
freq_out(iris$Species) #Does not accept objects other than factors a <- c(1:10) freq_out(a)
detect_char
This function tires to detect if a variable could be of class character. It takes in a vector:
convert
is set to be TRUE
, then return a the vector with its class redefined to be characterx <- c(1, 2, 3) detect_char(x) x <- c("This", "is", "actually", "a", "sentence") detect_char(x) x <- as.factor(x) class(x) detect_char(x) x <- detect_char(x, convert = T) x class(x) #Cannot accept weird input x <- as.data.frame(x) detect_char(x)
desc_reorder
This function reorders factor levels according to the descending order of the provided corresponding numeric variable.
x <- factor(c('a', 'b', 'c', 'd')) y <- c(2, 1, 4, 3) levels(x) x <- desc_reorder(x, y) levels(x) boxplot(Sepal.Width ~ Species, data = iris) boxplot(Sepal.Width ~ desc_reorder(Species, Sepal.Width), data = iris) #The first argument must be factor and the second must be numeric x <- c(2, 1, 4, 3) y <- factor(c('a', 'b', 'c', 'd')) desc_reorder(x, y) x <- as.factor(x) desc_reorder(x, y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.