Library contents

R is.integer

Check whether an imported fare has R's integer storage type before using it as integer cents.

A simple example

is.integer

  1. Start with Text "2" where an integer is required
  2. Do this Check or cast the type
  3. Result Type mismatch
A string containing a digit is still a string. Parsing would be a separate operation.

Syntax

R syntax
is.integer(fare)
Run these examples in R

Save one example at a time as Lesson.R. Use R 4.x.

Rscript --vanilla Lesson.R

Check the type of an imported fare

A transit app imports fares from two ticket machines. One machine sends the word “free” in a field normally filled with a whole number, so the importer needs to flag the record for review.

From the programfare <- "free"
ResultReview fare type
Lesson.R
fare <- "free"
if (length(fare) == 1L && is.integer(fare) && !is.na(fare)) {
    cat(fare, "\n")
} else {
    cat("Review fare type\n")
}
Output
Review fare type

The imported value holds text. The desk flags the fare before treating it as a number.

Advertisements

Import a museum headcount

The counter sends the number 36 as an integer object. The import expects an integer value. Flag a text field for review before calculating a total.

GivenInteger 36
Worked resultNumber accepted

Review a quoted fare

A new terminal sends its fare as text in a field that previously carried an integer. The import expects an integer value. Flag a text field for review before calculating a total.

GivenText “36”
Worked resultReview field type

What to watch for

A whole-valued double does not pass is.integer. Choose the check that matches the file's schema.

Documentation for is.integer