This article outlines the various types of data and how to use them in your R code. This is helpful when working with variables from a data set as well as creating custom R Calculations on a page. This article covers the following types of data:
Get a copy of the examples above in your account by clicking HERE.
Requirements
- An R variable, calculation, or data set.
- If referencing variables from a data set, familiarity with the different Structures and Value Attributes for Variable Sets and how their data is pulled through in R (i.e. Nominal variable sets are factors in R). See Technical Details for a handy reference.
Method
Knowing the type of data you are working with in R is useful because certain functions require specific data types for inputs/outputs. For example, you can't perform mathematical operations on numbers that have a character data type. To see what data is being used by R (the numeric value or label/string), hover over the variable name in the code and view the data in the preview. Below is a list of the various data types and examples of what you can do with each.
Note, a few functions used throughout: head() - shows just the first part of the data, cbind() - combines data into columns, rbind() - combines data by rows.
1. Logical
A logical data type will always return TRUE or FALSE. This can be created by a condition (logical test) and some functions. In Displayr, logical variables can also be used as binary filters.
Below are examples that return a logical result:
When the final result is a list of T/F results, Displayr shows them as Xs (false) and check marks (true).
You can use logical results to create a series of if .... else statements.
You can use conditions to subset data in R. When you put a condition inside square brackets (as described in How to Work with Data in R), the TRUE/FALSE (T/F) results are used to select the data that is TRUE for the condition. In the example below, on Line 2, we create a vector of data (called a) equal to: 1, 2, 3. On line 4, the a > 2 returns F, F, T inside the brackets to select the number 3 in a. The rest of line 4 changes the number 3 to a 10, which is then displayed in the final result.
2. Numeric
A numeric data type is a number that is not encompassed in "" when Show raw R output is checked:
Above, the first 3 numbers are numeric, while the last 3 are text. Note, numeric formulas will only work on data with the correct data type. In Displayr, only numeric and binary data will allow you to adjust decimal places. Variables that have a Structure of Numeric, Numeric-Multi, Numeric-Grid, Binary-Multi, and Binary Grid, as well as drag and drop banners, will be treated as numeric data in R.
3. Character
A character is text or string. This will always be encompassed in "" when Show raw R output is checked. When Displayr sees "z", it will view it as text. However, writing z in your code will lead Displayr to believe you are referencing an R object called z.
Variables that have a Structure of Text, and Text-Multi will be treated as strings/character data in R. Also, although Nominal, Nominal-Multi, Ordinal, and Ordinal-Multi variable sets are factors (as explained below) the character labels of their categories is used in logical conditions.
4. Date
A date can be either a Date or Date/Time object.
as.Date("2021-05-20") # Dateas.POSIXct("2021-05-20") # Date/timeDate/Time variables in Displayr are POSIXct so they can store a date or date and time. R will pull in the raw dates, but you can view aggregated dates by using the following code:
attr(yourdate,"QDate")5. Factor
A factor in R is equivalent to a category in data. This is equivalent to Nominal or Ordinal variables in Displayr. A factor contains both a value and a label. These are called levels, which can be referenced using levels(x). Levels can also be viewed in a raw output:
The examples below produce the data as labels and values separately:
as.character(Gender) # labelas.numeric(Gender) # valueNote, if your data is ordinal, it will appear as an ordered factor.
If the categories have been merged, this merging will be reflected in the way the data appears in R. This is done as follows:
- If all the categories of the variable are mutually exclusive and exhaustive, they all appear in R.
- Any categories that are missing (i.e., hidden) are inserted, such that the categories are mutually exclusive and exhaustive.
Particulars of using factors in your code:
- When using factors in code, you can treat them as character variables:
- However, when using factors in certain functions like cbind and rbind, you'll need to convert them to character first:
- Sorting and ordering will use the sequential levels not the labels:
To access the underlying coded values, use the attr() function to get the "values" attribute. You can also get the underlying coded values for each record by referencing the categories' order in brackets:
- Certain mathematical functions like max can only be run using Ordinal variables because you can't take a max of unordered categories (like male, female).
Technical Details
Each Variable Set Structure has a corresponding R structure/data type, and it is important to know how R handles each. The easiest way to see what "R sees" as the data is to hover over the highlighted name of the variable in your code to see a preview of the data being sent. Also keep in mind, unless you hide the category in a table, NETs and SUMs will also be returned with the variable set. Referencing a variable set with multiple variables will return a data.frame of all the variables (columns).
The table below explains how R handles each variable Structure and tips for working with each in Displayr for reference:
| Variable Set Structure |
Data type / Structure in R |
Notes |
| Nominal or Nominal-Multi | Factor |
Cannot be used with mathematical operations and conditions. Results will be blank or will use the factor level -- not the underlying Value of the Value Attributes. To use the underlying Values in the Value Attributes in a calculation, it's easiest to make a Duplicate of the variable and change the Structure to Numeric. You can also access this information in the values attribute (i.e. To use category labels as numbers, convert to character then numeric (i.e. |
| Ordinal or Ordinal-Multi or Experiment | Ordered Factor | Same as Nominal except can use max() and min() to find the highest and lowest category in the order. |
| Numeric or Numeric-Multi or Numeric-Grid or Ranking | Numeric | Can use this in mathematical calculations, but be aware many base R functions do NOT ignore missing NA data. You can also consider using Displayr mathematical functions, which by default ignore missing data. |
| Text or Text-Multi | Character | There are many functions that can help you work with character data in R in the stringr package and paste() are most commonly used. |
| Date/Time | POSIXct format |
Can be used in comparisons and other functions to calculate and manipulate dates mathematically (i.e. return the quarter of the date or add 2 days to the date). The individual date is used - not the aggregation. To return the date labels for the aggregation that you have set in the Date/Time settings in Displayr (i.e. Jan-Mar 26) use Can use |
| Binary-Multi or Binary-Grid or Binary-Multi (Compact) |
Numeric (binary values: 1s, 0s, and NA) |
These are commonly how multi-response survey questions are formatted. You can use SumEachRow() to see how many selections each respondent made, and also AnyOfEachRow() across selected variables to make a custom NET variable. |
Next
Using R in Displayr Video Series
How R Works Differently in Displayr Compared to Other Programs
How to Use Point and Click Inside R Code
How to Reference Different Items in Your Document in R
How to Work with Conditional R Formulas
How to Extract Data from a Single Column Summary Table
How to Extract Data from a Multiple Column Table
How to Extract Data from a Multiple Column Table with Multiple Statistics
How to Extract Data from a Multiple Column Table with Nested Data
UPCOMING WEBINAR: From Thousands of Tables To One Clear Story: using AI in survey analysis