Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

toVariable

Generic method for converting objects to Crunch representations


Description

R objects are converted to Crunch objects using the following rules:

Usage

toVariable(x, ...)

## S4 method for signature 'CrunchVarOrExpr'
toVariable(x, ...)

## S4 method for signature 'character'
toVariable(x, ...)

## S4 method for signature 'numeric'
toVariable(x, ...)

## S4 method for signature 'factor'
toVariable(x, ...)

## S4 method for signature 'Date'
toVariable(x, ...)

## S4 method for signature 'POSIXt'
toVariable(x, ...)

## S4 method for signature 'AsIs'
toVariable(x, ...)

## S4 method for signature 'VariableDefinition'
toVariable(x, ...)

## S4 method for signature 'logical'
toVariable(x, ...)

## S4 method for signature 'labelled'
toVariable(x, ...)

## S4 method for signature 'haven_labelled'
toVariable(x, ...)

## S4 method for signature 'labelled_spss'
toVariable(x, ...)

## S4 method for signature 'haven_labelled_spss'
toVariable(x, ...)

Arguments

x

An R vector you want to turn into a Crunch variable

...

Additional metadata fields for the variable, such as "name" and "description". See the API documentation for a complete list of valid attributes.

Details

  • Character vectors are converted into Crunch text variables

  • Numeric vectors are converted into Crunch numeric variables

  • Factors are converted to categorical variables

  • Date and POSIXt vectors are converted into Crunch datetime variables

  • Logical vectors are converted to Crunch categorical variables

  • VariableDefinition()s are not converted, but the function can still append additional metadata

If you have other object types you wish to convert to Crunch variables, you can declare methods for toVariable.

Value

A VariableDefinition object. To add this to a dataset, either assign it into the dataset (like ds$newvar <- toVariable(...)) or call addVariables(). If you're adding a column of data to a dataset, it must be as long as the number of rows in the dataset, or it may be a single value to be recycled for all rows.

See Also

Examples

var1 <- rnorm(10)
toVariable(var1)
toVariable(var1, name = "Random", description = "Generated in R")
## Not run: 
ds$random <- toVariable(var1, name = "Random")
# Or, this way:
ds <- addVariables(ds, toVariable(var1, name = "Random"))

## End(Not run)

crunch

Crunch.io Data Tools

v1.28.0
LGPL (>= 3)
Authors
Greg Freedman Ellis [aut, cre], Jonathan Keane [aut], Mike Malecki [aut], Neal Richardson [aut], Gordon Shotwell [aut]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.