# Variable

## *class* <mark style="color:purple;">Variable</mark>

Variables represent the "columns" in tables (and uploads), and have certain metadata as well as detailed univariate summary statistics associated with them. Use this class to interface with variables and their metadata.

## Constructors

<table data-header-hidden><thead><tr><th width="342">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="query/queryusdlist_variables"><strong><code>Query$list_variables</code></strong></a>([max_results])</td><td>List all variables in a (completed) query's results.</td></tr><tr><td><a href="table/tableusdlist_variables"><strong><code>Table$list_variables</code></strong></a>([max_results])</td><td>List all variables in a table.</td></tr><tr><td><a href="upload/uploadusdlist_variables"><strong><code>Upload$list_variables</code></strong></a>([max_results])</td><td>List all variables in a (completed) upload.</td></tr><tr><td><a href="../../../rest-api/variables"><strong><code>Query$variable</code></strong></a>(variable_name)</td><td>Construct a reference to a variable within a completed query based on its (case-insensitive) name.</td></tr><tr><td><a href="../../../rest-api/variables"><strong><code>Table$variable</code></strong></a>(variable_name)</td><td>Construct a reference to a variable within a table based on its (case-insensitive) name.</td></tr><tr><td><a href="../../../rest-api/variables"><strong><code>Upload$variable</code></strong></a>(variable_name)</td><td>Construct a reference to a variable within a completed upload based on its (case-insensitive) name.</td></tr></tbody></table>

## Examples

{% tabs %}
{% tab title="Basics" %}

<pre class="language-r"><code class="lang-r">loadNamespace("redivis")

<strong>table &#x3C;- redivis$organization("Demo")$dataset("iris_species")$table("iris")
</strong>
variable &#x3C;- table$variable("sepalLengthCm")

variable$exists() # -> TRUE
variable$get() 

print(variable$properties)

{
	"kind": "variable",
	... (see variable resource definition)
}

print(variable$get_statistics())

{
  "kind": "variableStatistics"
  "status": "completed",
  "count": 150,
  "numDistinct": 35,
  "min": 4.3,
  "max": 7.9,
  "mean": 5.8433333333333355,
  "approxMedian": 0.8280661279778625,
  ...
}
</code></pre>

{% endtab %}
{% endtabs %}

## Attributes

| **`properties`** | A named list containing the [API resource representation of the variable](https://docs.redivis.com/api/resource-definitions/variable#base-definition). This will only be populated after certain methods are called, particularly the `get` method, and will otherwise be an empty list. |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`table`**      | A reference to the [Table](https://docs.redivis.com/api/client-libraries/redivis-r/reference/table) instance that constructed this variable. Will be `NULL` if the variable belongs to an upload.                                                                                        |

## Methods

<table data-header-hidden><thead><tr><th width="421">Name</th><th>Description</th></tr></thead><tbody><tr><td><a href="variable/variableusdget"><strong><code>variable$get</code></strong></a>()</td><td>Gets the variable, after which the <code>properties</code> attribute will be populated according to the <a href="../../../../resource-definitions/variable#get-definition">variable.get resource definition.</a></td></tr><tr><td><a href="variable/variableusdget_statistics"><strong><code>variable$get_statistics</code></strong></a>()</td><td>Gets the variable statistics, returning a named list corresponding the <a href="../../../resource-definitions/variablestatistics">variableStatistics resource definition.</a></td></tr><tr><td><a href="variable/variableusdexists"><strong><code>variable$exists</code></strong></a>()</td><td>List all visible datasets owned by the user.</td></tr><tr><td><a href="variable/variableusdupdate"><strong><code>variable$update</code></strong></a>([label, description, ...])</td><td>Update a variable's metadata.</td></tr></tbody></table>
