Data Import and Preparation

  • Data Preparation

    Common data preparation tasks pacman::p_load(tidyverse, magrittr, haven, install = FALSE) Dealing with NAs replace_na() Replaces any NA value with the desired character or integer. Requires character or integer class vector Does not work with factors, use fct_explicit_na() Does not work with labelled variables, convert to numeric or character first Multiple variables

  • Import data into R

    R can import a number of different file types for data wrangling or analyses. The following is a go-to guide for importing .csv, Excel, SPSS, .txt, and .dat files. Occassionally, additional packages may be require to import a specific type of file.

  • Converting wide to long data in R

    Structured datasets tend to come in one of two formats, wide or long. In the wide format, each row contains data related to a particular subject or event. Each values in each column usually represent a variable taken from that participant.

  • Converting long to wide data in R

    In the previous guide, we took a wide data set and converted it to long. While this conversion allowed us to apply some statistical and plotting functions on the data, there may be cases where doing the reverse, going from long to wide, may be useful.