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 Anything > Table > Raw Data > Variables. See How to Export Raw Data to Excel for details on creating the latter. In this example, our table is called cola.raw.data.
Method
1. Select Calculation > Custom Code.
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,]
See Also
How to Work with R in Displayr
Comments
0 comments
Article is closed for comments.