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.
Can we soften some of their sharp edges?
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)
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)
The freorder()
function returns a factor in a descending order. Let's see an exmaple from our previous homework:
# first we load the gapminder first suppressPackageStartupMessages(library(gapminder)) suppressPackageStartupMessages(library(tidyverse)) summary(gapminder) # the original order for continent is alphabetical order. gapminder %>% select(lifeExp,continent) %>% ggplot(aes(continent,lifeExp))+ geom_boxplot(alpha=0.25, aes(fill=continent))
# now we apply the freorder() function to the continent, and we are expecting a reversed order. gapminder %>% select(lifeExp,continent) %>% ggplot(aes(continent,lifeExp))+ geom_boxplot(alpha=0.25, aes(fill=freorder(continent)))
The fset_level()
function sets levels to the order in which they appear in the data, i.e. set the levels “as is”:
And again we use the same example, but compare it with the one applied with fset_level()
.
# the original order for continent is alphabetical order. gapminder %>% select(lifeExp,continent) %>% ggplot(aes(continent,lifeExp))+ geom_boxplot(alpha=0.25, aes(fill=continent))
# now we apply the fset_level() function to the continent, and we are expecting an order which is the same as the entry appeared in the data frame. gapminder %>% select(lifeExp,continent) %>% ggplot(aes(continent,lifeExp))+ geom_boxplot(alpha=0.25, aes(fill=fset_level(continent)))
The df_write()
write a dataframe into the file
df <- data.frame( testing = factor(c("a","b","c","a","c","e"), levels = c("a", "b","c","e"), labels = c("A", "B","C","E"))) df_write(df, "./test.txt")
The df_read()
then read the data from the file and retating the factor levels.
(a <- df_read ("test.txt")) # check its structure to see whether the factor level retained. str(a)
The df_write()
write a dataframe into the file. we can write the above dataframe to file
df_write(a, "./gapMinderTest.txt")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.