Filtering Data

Previous: analyzing data


Sometimes you only want to work with a subset of your data. With the crunch package, you can both filter the views of data you work with in your R session and manage the filters that you and your collaborators see in the web application.

Filtering and subsetting in R

As we've seen in previous vignettes, making logical expressions with Crunch datasets and variables is natural. We showed how to update a selection of values on the server, as well as how to crosstab in a subset of a dataset. Other applications work just as intuitively.

Filtering like this works by creating a dataset or variable object that has the filter embedded in it:

dems <- ds[ds$pid3 == "Democrat",]
cat(printdems, sep="\n")
round(crtabs(mean(track) ~ educ + gender, data=dems), 2)
round(tab8, 2)

When you extract a variable from a filtered dataset, it too is filtered. So


is the same as

table(ds$educ[ds$pid3 == "Democrat",])

As an aside, if you prefer using the subset function, that works just the same as the [ extract method on datasets:

identical(subset(ds, ds$pid3 == "Democrat"), dems)
identical(dems2, dems)

Filter entities

In the web application, you can save filter definitions with names for easy reuse. You can also share these filter definitions with other viewers of the dataset.

To do so, we work with the dataset's filter catalog. To start, our filter catalog will be empty:


Create a filter by assigning a Crunch logical expression to the catalog by the name we want to give it, using $ or [[:

filters(ds)[["Young males"]] <- ds$gender == "Male" & ds$age < 30
filters(ds)[["Young males"]]
cat(print.young.males1, sep="\n")

This new filter now appears in our filter catalog.


You could also have made the filter with the newFilter function:

f <- newFilter("Young males", ds$gender == "Male" & ds$age < 30)

This filter is now available for you to use in the web application. If you want to make the filter available to all viewers of the dataset, make it "public":

is.public(filters(ds)[["Young males"]]) <- TRUE

You can also edit the filter expressions by assigning a new one in, like:

filters(ds)[["Young males"]] <- ds$gender == "Male" & ds$age < 35
filters(ds)[["Young males"]]
cat(print.young.males2, sep="\n")

Exclusion filters

One other application for filtering is the dataset exclusion filter. The exclusion allows you to suppress from view rows that match a certain condition. Exclusions are set on the dataset with a Crunch logical expression:

exclusion(ds) <- ds$perc_skipped > 15
cat(high_perc_skipped, sep="\n")

All viewers of the dataset will see the dataset as if those rows do not exist; however, as the editor of the dataset, you can remove the exclusion filter to see them if you need:

exclusion(ds) <- NULL

Alternative: dropRows

If you do know that you never want to see those rows again, you can permanently delete them with dropRows:

## Not run
ds <- dropRows(ds, ds$perc_skipped > 15)

Next: exporting data

Try the crunch package in your browser

Any scripts or data that you put into this service are public.

crunch documentation built on Jan. 11, 2019, 1:06 a.m.