expressions {crunch} | R Documentation |
Construct Crunch Expressions from Crunch Database Functions
Description
Crunch Expressions, i.e. CrunchExpr
and CrunchLogicalExpr
,
encapsulate derivations of Crunch variables, possibly composed of other functions
which are only evaluated when sent to the server when creating a variable using VarDef()
or using as.vector()
to get data. The crunch database functions can be found in the
Help Center,
and can be called directly via crunchdbFunc()
m but many have also been wrapped
in native R functions, and are described in the details section below.
Usage
crunchdbFunc(fun, x, ...)
Arguments
fun |
The name of the crunch database function to call |
x |
An input, a crunch variable, expression or R object |
... |
Other arguments passed to the database function |
Details
Logical expressions
These logical operators
==
,!=
,&
,|
,!
,%in%
work the same way as their base R counterparts-
is.selected(x)
returnCrunchLogicalExpr
whether a value is in a selected category -
rowAny(x)
androwAll(x)
work row-wise onMultipleResponse
Variables (and expressions), thoughna.rm
is not implemented forall(x)
.%ornm%
is similar to|
, but where "not selected" beats "missing" (soFALSE %ornm% NA
isFALSE
instead ofNA
as it would be withFALSE | NA
)
Comparisons
Comparison operators
<
,<=
,>
,>=
work the same way as their base R counterparts.-
crunchBetween(x, lower, upper, inclusive)
to provide lower and upper bounds in a single expression.
Missing data expressions
-
is.na(x)
,is.valid(x)
returnCrunchLogicalExpr
whether a single variable (or expression that creates one) is missing (or not missing). -
rowAnyNA(x)
,rowAllNA(x)
returnCrunchLogicalExpr
whether any/all values in an array variable (or expression that creates one) are missing. -
complete.cases(x)
returns an expression that is "selected" if all cases are non-missing, "missing" if they are all missing, and "other" otherwise.
Selection expressions
-
selectCategories(x, selections, collapse = TRUE)
takes a categorical variable (or array) and marks categories as selected.selections
should be a list of category names or values. Ifcollapse
isTRUE
, (the default), it collapses the categories to "selected", "other" and "missing", but it isFALSE
, then the old categories are preserved. -
asSelected(x)
returns an expression that condenses a categorical into 3 categories ("selected", "other" or "missing") -
selectedDepth(x)
returns an expression that creates a numeric variable that counts the number of selections across rows of an array variable (or expression that creates one) -
arraySelections(x)
returns an expression that takes an array and creates an array with each variable condensed to "selected", "other" or "missing" and an extra subvariable "any" that indicates whether any is selected. -
alterCategoriesExpr(x, categories = NULL, category_order = NULL, subvariables = NULL)
Change the category names, order, or subvariable names of categorical or Array variables (can only modify existing ones, not add or remove categories or subvariables).categories
is aCategories
object or a list of lists, each with aname
indicating the new name, as well as anid
orold_name
to identify which category to modify.category_order
is either a numeric vector indicating category ids or a character vector indicating the names of the categories in the order they should be displayed (note that all categories must be specified).subvariables
is a list of lists, each with aname
to rename the subvariable and analias
,old_nam
orid
to identify the subvariable. Whenx
is an expression, all categories and subvariables must be identified byid
.
Array expressions
-
makeFrame(x, numeric = NULL)
an expression that creates an array from existing variables or expressions, seederiveArray()
for more details -
arraySubsetExpr(x, subvars, subvar_id = c("alias", "name", "id"))
Take a subset of an existing array variable, identifying the subvariables by alias, name, or id (ifx
is an expression, you must use id). -
alterArrayExpr( x, add = NULL, order = NULL, order_id = c("alias", "name", "id"), remove = NULL, remove_id = c("alias", "name", "id"), subreferences = NULL, subreferences_id = c("alias", "name", "id") )
Add, reorder, remove or rename subvariables on an an array variable
x
. Theadd
argument is a list of variables or expressions, optionally named with the id they should have.order
andremove
are vectors of aliases, names or ids (specify which withorder_id
/remove_id
). Thesubreferences
object is a list of lists that are named the alias, name, or id (again specify which withsubreferences_id
) with metadata information like name and alias in the list.
Miscellaneous expressions
-
caseExpr(..., cases)
Create a categorical variable from a set of logical expressions that when met are assigned to a category. SeemakeCaseVariable()
for more details. -
bin(x)
returns a column's values binned into equidistant bins. -
nchar(x)
returns a numeric value indicating the length of a string (or missing reason) in aTextVariable
(or expression that creates one) -
unmissing(x)
for aNumericVariable
(or expression that creates one) return the values of the data, ignoring the ones set to missing. -
trim(x, min, max)
for aNumericVariable
(or expression that creates one) return values that where all values less thanmin
have been replaced withmin
and all values greater thanmax
have been -
crunchDifftime(e1, e2, resolution)
Gets the difference between two datetimes as a number with specified resolution units (one ofc("Y", "Q", "M", "W", "D", "h", "m", "s", "ms")
). -
datetimeFromCols(year, month, day, hours, minutes, seconds)
create aDatetime
variable from numeric variables or expressions (year
,month
, andday
are required, buthours
,minutes
, andseconds
are optional) -
rollup(x, resolution)
sets the resolution of a datetime variable or expression, seeresolution()