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

This is the repository for ZHENYI HUANG's HW07.

Please refer to the following table for easy access:

| List of the Files | |--------------------| | R script file| | Description files| | Test files| | Vignettes file

NOTE:

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("STAT545-UBC-students/hw07-janehuang1647")

Quick demo

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 factor_check() function returns a stopping message if the input is not a factor:

factor_check(x)

The freorder()function returns a factor in a descending order:

freorder(x)

The fset_level() function sets levels to the order in which they appear in the data, i.e. set the levels “as is”:

y <- c("apple","coconut","banana")
yfx <- factor(y)
#check bothe the level of the original factor and after the fset_level() function.
levels(yfx) # the levels is ordered in alphabetical order
levels(fset_level(yfx))  # the level is ordered in the order of the original factor.

The df_read() then read the data from the file and retating the factor levels.

(a <- df_read ("gapminderTest.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")


STAT545-UBC-students/hw07-janehuang1647 documentation built on May 30, 2019, 12:50 p.m.