typestar

Types and coercion in R

R's atomic types and the rules that convert between them.

count <- 42L
ratio <- 3.14
label <- "typestar"
flag <- TRUE
missing <- NA

is_int <- is.integer(count)
as_double <- as.numeric(count)
as_text <- as.character(ratio)
kind <- class(label)

mixed <- c(1, "two", TRUE)
coerced <- class(mixed)

How it works

  1. 42L is integer; plain 3.14 is double.
  2. as.numeric and as.character convert explicitly.
  3. Mixing types in c() silently coerces to the widest one.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
237
Tokens
57
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 43 seconds.

Type this snippet

Step 2 of 4 in Vectors & types, step 2 of 20 in Language basics.

← Previous Next →