check_sID | R Documentation |
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
.
check_sID(x)
pad_zeros(x)
x |
|
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.
check_sID
returns either the input vector or a character
vector padded with 0
A character vector with zero-padded values
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'
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.