This documents contains detailed information of the functions in the foofactorsran
package. fbind()
and freq_out()
were left unchanged.
age_group()
takes a numeric vector (ages) with each element in the vector bigger than 0 and smaller than 120 to be a valid age. It returns a factor of the same length as the input and replaces each age by its age group. The age groups are defined as follows:
If droplevel = FALSE
, the levels for x will be c("Children","Youth","Adult","Senior")
. If droplevel = TRUE
, which is also the default setting, the unused levels of age groups will be dropped, but the order of levels stay the same.
Example:
library(foofactorsran)
x <- c(2,45,22,27) age_group(x) age_group(x,droplevel = T) age_group(x,droplevel = FALSE)
The following input would produce an error.
oops1 <- age_group(c(12,56),droplevel = 2) oops2 <- age_group(c(-1,56))
factor_level()
is a function that converts the input x to a factor (if it is not a factor ) and defines the order of factor levels at the same time. The level
argument is set to "alp"
by default which means the levels will be ordered alphabetically. If level = "asit"
, the factor levels will be set in the order they appear in x. User can also define the level order the way they define in the function factor()
, other arguments of factor()
can be passed to factor_level()
as well.
To avoid turning a value of x to "NA" by defining levels (like factor()
sometimes does), only level with the same length as original number of levels (or number of levels of as.factor(x) if x is not originally a factor), and the level must include all values of x. Note that factor_level()
would not drop unused levels if the input x is a factor.
Examples:
x <- factor(c("b","b","a","c","d","a"),levels = c("c","a","b","d","e")) factor_level(x, level = c("e","d","c","b","a")) factor_level(x) factor_level(x,"asit")
The following will produce an error.
y <- c("aa","aa","cc","bb","cc") z <- as.factor(y) factor_level(y, level = c("aa","cc")) factor_level(z, level = c("aa","cc")) factor_level(y, level = c("red","green","blue")) factor_level(z, level = c("red","green","blue"))
Factor_level() can pass arguments of the function factor()
only if the level
argument is user defined. To see an example:
v <- c("apple","apple","orange","pear") factor_level(v,level = c("apple","pear","orange"), labels = c("red","orange","yellow"))
order_asit()
is a function that takes a vector of characters (or a factor) and returns a factor with order of levels defined in the way they appear in x. order_asit(x)
is equivalent to factor_level(x,"asit")
. Note that a warning would show up if the input has many unique values (same thing applies to factor_level()
).
w <- as.factor(c("a","b","d","c","e")) order_asit(w)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.