View source: R/unpackUserVariable.R
unpackUserVariable | R Documentation |
Split a user defined variable (from a QuestBack custom question), where multiple responses are stored in one column (e.g. "1*3*5*7") into multiple columns (e.g. 1, 3, 5, 7)
unpackUserVariable(rawText, maxCols = NULL, emptyReplacement = -99, numeric = FALSE, delimiter = "*")
rawText |
A (character) vector that you want to split |
maxCols |
(optional) If rows have different lengths, then this should define the maximum number of columns returned. If missing this will be guessed. |
emptyReplacement |
(optional) value to use when some rows are shorter than maxCols (default is -99) |
numeric |
(optional) When set to TRUE, will attempt to convert output to numeric matrix (default is FALSE) |
delimiter |
(optional) character used to split elements in each row (default is *) |
Sometimes the output from a custom question on QuestBack is stored as a string representing multiple values e.g. "1*3*5*7". This function takes a vector of values stored in this format and splits them out into a matrix of values. If the rows have variable lengths, then each row will be padded out using the emptyReplacement value.
Returns a matrix with same number of rows as input. If input had variable lengths and maxCol was not set, then returns a list.
See also strsplit
# most common usage...
test1 = c("a*1*2*3","b*10*20*30","c*11*22*33","d*1*2*3")
unpackUserVariable(test1)
# if you know the input is numeric then can convert..
test2 = c("1*2*3","10*20*30","11*22*33","1*2*3")
unpackUserVariable(test2,numeric = T)
# if input has variable number of elements per row..
# ...then set maxCols to match the longest row...
# ...but the function should guess maxCols if not provided...
test3 = c("1*2*3","10*20","11*22*33","1*2*3")
thisResult = unpackUserVariable(test3,numeric = T, maxCols = 3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.