knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

In this vignette we'll discuss the functions and data contained within this package and explain the use cases for each component.

Introduction

This package was designed to aide users in manipulating the order and orientation of data frames. The namesake function of this package, flip(), was inspired by data generated when analyzing model adequacy of a set of character data to a phylogenetic tree. Included in this package is a data frame containing a snippet of the data generated by said analysis; this data is shown below.

library(flipR)

simulation_data

As you can see, the orientation of rows and columns is not conducive for data manipulation, wrangling, or plotting. This package provides tools for the user to solve this problem.

flip()

Base R contains the function t(), which technically solves the problem explained above. It switches the rows and columns of any matrix-like object inserted into the function. However, it has the unlucky side effect of converting the data frame into a less useful form; a matrix. In essence, flip() is a wrapper for base R t() but converts the result back into a more useful data frame.

#t() flips the rows and columns but results in a matrix
class(t(simulation_data))

#flip() returns a data frame
class(flip(simulation_data))

To aid the user in sliding this into their already-existing scripts, the flip() function, as well as all other functions in this package, can optionally return the data in the form of a tibble from the tibble package or data table from the data.table package.

#An additional argument can convert the output into other data frame forms
class(flip(simulation_data, "DT"))
class(flip(simulation_data, "TB"))

The invert functions

In addition to flipping the rows and columns of a data frame object, one might need to invert the order of rows or columns. For example, in the iris data set, it might be more helpful to see the species and petal columns first.

head(iris)
head(invert_col(iris))

Alternatively, it might be helpful to see later rows first. For example, in the USPersonalExpenditure data set one might want to see more Private Education first, so then I'd use the invert_row() function.

head(USPersonalExpenditure)
head(invert_row(as.data.frame(USPersonalExpenditure)))

Just like the flip() function, the invert functions can also output data as tibbles or data tables.

invert_col(iris, "DT")
invert_row(iris, "TB")


fieldima/flipR documentation built on Dec. 20, 2021, 8:40 a.m.