A very common way to use R in Displayr is for filtering. There are 3 methods:
- Method - Filter raw data in a custom Calculation via the Filter(s) field
- Method - Filter raw data in a custom Calculation via code
- Method - Create an R filter variable to apply to outputs
This article specifically describes how to go from a table of raw data...
...to a filtered table based on a defined condition within the code of the Calculation:
Requirements
- A raw data table
- Can create a data.frame of raw data in a custom Calculation using something like data.frame(q1a, q3), see How to Work with Data in R.
- Can use a native table of a single variable set showing Columns > RAW DATA.
- Or use Anything
> Table > Raw Data > Variables. See How to Export Specific Raw Data Variables to Excel for details on creating the latter. In this example, our table is called cola.raw.data.
- An understanding of how to construct conditional formulas in R, see How to Work with Conditional R Formulas and How to Use R Code to Create a Filter Based on Single-Response Questions.
Method - Filter raw data in a custom Calculation via the Filter(s) field
If you are using variables from your Data Set in your custom R code, you can apply any filter from the data set to filter the data for your Calculation. You need to use the global QFilter
variable in your code to subset your raw data, see How to use the Filter(s) Dropdown to Quickly Filter R Outputs using QFilter for detail.
Method - Filter raw data in a custom Calculation via code
You can also write R code within the custom Calculation to filter the raw data it uses. In this example, we wish to filter our table by those who chose Coca-Cola as their preferred cola.
1. Select Calculation > Custom Code.
2. Paste the below under General > R Code:
#identify the table being used name it df for the code
#or create one with data.frame(variablename1, variablename2)
df = cola.raw.data
#use brackets to subset the rows (before the ,) of the table where the second column
#df[,2] is equal to Coca-Cola. The %in% operator accounts for missing data
#You can put other values separated by , within quotes in the c()
new.df = df[df[,2] %in% c("Coca-Cola"),]
Note, you can also add further items to the filter condition as follows:
#rows where the second columns contains Coca-Cola or Coke
df[,2] %in% c("Coca-Cola","Coke")
If you have multiple conditions, it's useful to define those outside the bracket subsetting first:
#identify the raw data table being used name it df for the code
df = cola.raw.data
#keep coke drinkers
coke = df[,2] %in% c("Coca-Cola","Coke")
#remove pepsi drinkers
nopep = !df[,2] %in% c("Pepsi")
#use brackets to subset the rows (before the ,) of the table that meet criteria above
new.df = df[coke & nopep,]
Method - Create an R filter variable to apply to outputs
To create a filter variable that can filter your raw data table (and other outputs), see How to Use R Code to Create a Filter Based on Single-Response Questions.
Next
How to Export Specific Raw Data Variables to Excel