pad_zeros: Test if an object is a character vector of numbers

View source: R/utils.R

check_sIDR Documentation

Test if an object is a character vector of numbers

Description

check_sID is a convenience function to test if a vector (typically the subject ID column in a data.table) is a character vector of numbers, a factor vector of numbers, or a numeric vector. If so, it will zero-pad the variable to have equal width.

pad_zeros pads a vector with zeros to avoid issues with ordering a column of integers or integers converted to character.

Usage

check_sID(x)

pad_zeros(x)

Arguments

x

pad_zeros accepts either a vector (numeric or character) or a single integer. check_sID accepts a character, numeric, or factor vector

Details

This function is meant to avoid issues that arise when sorting a vector of numbers that have been converted to character. For example, import_scn automatically reads in the first column (with FreeSurfer outputs this is the column of subject IDs) as a character variable. If the subject IDs had been all numbers/integers, then sorting (i.e., setting the key in a data.table) would be incorrect: e.g., it might be '1', '10', '2', ....

If “x” is a numeric vector, then the resultant string width will be determined by max(x) or x itself if the input is a single integer. For example, if x=10, it will return '01', '02', ..., '10'. If “x” is a character vector, then the output's string width will be max(nchar(x)). For example, if x includes both '1' and '1000', it will return '0001', etc.

Value

check_sID returns either the input vector or a character vector padded with 0

A character vector with zero-padded values

Examples

pad_zeros(10)  # '01' '02' ... '10'
x <- c(1, 10, 100)
pad_zeros(x)   # '001' '010' '100'
x <- as.character(x)
pad_zeros(x)   # '001' '010' '100'

cwatson/brainGraph documentation built on Feb. 21, 2024, 6:33 p.m.