O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Checking for out-of-bounds data

It's common for numeric values in a column to have a natural constraint on the values that it should hold. For example, if a column represents a percent of something, we might want to check if all the values in that column are between zero and one (or 0 and 100). In assertr, we typically use the within_bounds function in conjunction with the assert verb to ensure that this is the case. For example, if we added a column to mtcars that represented the percent of the heaviest car's weight, the weight of each car is:

library(assertr) 
mtcars.copy <- mtcars 
 
mtcars.copy$Percent.Max.Wt <- round(mtcars.copy$wt /  
                                    max(mtcars.copy$wt), 
                                    2) 
 
mtcars.copy <- assert(mtcars.copy, within_bounds(0,1), 
                     Percent.Max.Wt) 

within_bounds ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required