create_factors: Use codebook to create factors and check levels for validity

Description Usage Arguments Details Value Author(s) Examples

Description

Uses a codebook which is an S3 class codebook, possibly read in by read_codebook, to convert numerical vectors into factors in a tibble.

Usage

1
create_factors(x, code_book, column_names = NULL)

Arguments

x

tibble to which codebook is applied

code_book

codebook containing factor names and factor levels to convert numeric or character vectors to factors and also test for valid levels.

column_names

character vector of column names for conversion to factors. Default: All factors defined in codebook.

Details

REPEAT: Often, when analysing data, data dictionaries or code books are provided with data files. Rather than a word doc or pdf files, the format required here is in a very specific format stored as a csv file. Once read in, attributes such as factor labels/levels and variable labels can be added to the data.frame and/or also used to check factor labels and variable names are consistent with the code book. Note that while various methods may be available which attempt to convert word docs or pdf's to a spreadsheet and/or csv file, extreme care should be taken as these are far from perfect.

Value

object of type class tibble

Author(s)

Peter Baker pete@petebaker.id.au

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
file.copy(system.file('demoFiles', 'data1_codebook.csv',
                      package='codebookr'), 'data1_codebook.csv')
file.copy(system.file('demoFiles', 'data1-birth.csv',
                      package='codebookr'), 'data1-birth.csv')
data1_codebook <- read_codebook("data1_codebook.csv",
         column_names = list(variable_levels = "Factor.Levels",
                             variable_original = "Old.Variable",
                             min = "Min", max = "Max"))
data1 <- readr::read_csv('data1-birth.csv')
data1
myData <- create_factors(data1,  data1_codebook)
str(myData)

petebaker/codebookr documentation built on May 6, 2019, 12:05 a.m.