Observe and check your data in R'

knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(dplyr)
library(tibble)
library(observer)

Create observations from your data with 'observe_if'

The observer package checks that a given dataset passes user-specified rules. The main functions are observe_if and inspect.

For instance, according to the documentation of the diamonds dataset in package ggplot2, the column depth is equal to 100*2*z/(x+y). Let us make an observation of this:

df <- ggplot2::diamonds %>% 
  mutate(depth2 = 100*2*z/(x+y)) %>% 
  observe_if(x > 0, 
             y > 0, 
             z > 0, 
             abs(depth-depth2) < 1)

obs(df)

We observe that 93 rows fail to satisfy this rule. To go further we need to see what is happening; with inspect we can select the rows at stake:

inspect(df, 4)

Another way is to write it with standard evaluation:

## Write your predicates first
p <- c(~ x > 0, ~ y > 0, ~ z > 0, 
       ~ abs(depth-depth2) < 1)

## Make observations
df %>% 
  observe_if_(.dots = p) %>% 
  obs()


Try the observer package in your browser

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

observer documentation built on May 1, 2019, 8:04 p.m.