Introduction
This article describes how to go from a raw data table...
...to a filtered table based on a defined condition:
Requirements
A raw data table with multiple columns created via custom code or Create > Tables> Raw Data > Variables. In this example, our table is called cola.raw.data.
Method
1. Select Create > R Ouput.
2. Update the Name on the object inspector under Properties > GENERAL.
3. Paste the below under Properties > R CODE:
df = cola.raw.data
new.df = df[df[,2] %in% c("Coca-Cola"),]
- In this example, we wish to filter our table by those who chose Coca-Cola as their preferred cola.
- Above, we filter our df table by rows where the data matches Coca-Cola in the second column using the %in% operator.
- Note, you can also add further items to the filter condition as follows:
c("Coca-Cola","Coke")
Alternatively, you can first define your filter condition using one of the below methods:
f = df[,2] %in% c("Coca-Cola")
f = df[,2] == "Coca-Cola"
Note, that using the above code only will result in a TRUE or FALSE based on whether the condition is met.
The last line can then be amended as follows, to filter the rows based on the true or false result stored in f.
new.df = df[f,]
Next
How to De-duplicate Raw Data Using R
How to Work with Conditional R Formulas
How to Use R Code to Create a Filter Based on Single-Response Questions
How to Create a New Variable Based on Other Variables using R
How to Recode Data Based on a Lookup Using R
How to Dynamically Filter Data to the Latest Waves