Repository navigation
Data Manipulation
Page Editor: @paigemiller
To request edits to this page, open an issue, and tag @paigemiller
80% of time spent on data analysis is for cleaning and preparing the data (Dasu and Johnson, 2003)
You have a data set and you want to get started preparing, summarizing, plotting, analyzing, and modeling... What should we be keeping in mind with respect to each process? Hopefully this section will point us to links for specific data manipulation tasks that we encounter from time to time.
Hadley Wickham, developer of R packages like reshape, ggplot2, stringr, and probably others, has advocated for scientists to strive for "tidy" data. He says, "Tidy datasets are all alike but every messy dataset is messy in its own way". Here's a link to his paper on Tidy Data and here is a tutorial on data tidying. Tidy data make summarizing, data visualization, and analysis nice and friendly.
Which R packages help you achieve Tidy Data?
-
magrittr -- piping functions to simplify and make code easier to read. We highly recommend learning the piping function early in your coding career even if you don't always use it. For example
my.data %>% my.functionis the same asmy.function(my.data)and sometimes when you want to string a few functions together to summarize and then plot using ggplot, you might not necessarily have to save the changes to the data. But if you do want to save the changes to the data your manipulating, you can usemy.data %<>% my.functionis the same asmy.new.data <- my.function(my.data). - dplyr -- lots of functions that you can also do in base R, but dplyr provides a small number of verb/functions that you can remember and return to often without having to look up what they do each time. My favorite cheat sheet is found here and it helps show you how to use the functions on an example data set.
- tidyr -- the cheat sheet above also helps with tidyr functions but here are some more resources. The last link brings you to an R community discussion about using Base R vs the tidyverse.
- reshape --
When writing code, we usually strive for a set of coding syntax rules laid out by the R user community at Google. Syntax in the coding world means the spelling and grammar of a programming language. The guide was designed to make code easier to read, share, and verify. In the Data Management Section, we recommended also saving files according to the Google Style Guide.
In some circumstances, we've found that following the Google Style Guide is not practical or beneficial. For example, for variables, variable.name is preferred in google style but variableName is "accepted". However, sometimes parsing code from different sources leads to problems with the period case style but not the camelback case style. Many people in the lab also use camelcase to separate variables/data objects from functions which we often name with a verb (like "get") then an underscore and then the object of interest (like "stats"): get_stats. [other circumstances we've encountered?]
Below is an example of a google style function. Notice the comments at the beginning, the procedure for output of an error message, and overall clarity of what the function does.
CalculateSampleCovariance <- function(x, y, verbose = TRUE) {
# Computes the sample covariance between two vectors.
#
# Args:
# x: One of two vectors whose sample covariance is to be calculated.
# y: The other vector. x and y must have the same length, greater than one,
# with no missing values.
# verbose: If TRUE, prints sample covariance; if not, not. Default is TRUE.
#
# Returns:
# The sample covariance between x and y.
n <- length(x)
# Error handling
if (n <= 1 || n != length(y)) {
stop("Arguments x and y have different lengths: ",
length(x), " and ", length(y), ".")
}
if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) {
stop(" Arguments x and y must not have missing values.")
}
covariance <- var(x, y)
if (verbose)
cat("Covariance = ", round(covariance, 4), ".\n", sep = "")
return(covariance)
}
- journal-club doc
- google-sites lab manual
- index of all Drake-lab google sites
- lab-meeting--minutes doc Contact John if you are having trouble accessing google docs or websites.
- repository of public domain images