Variables and types

Numbers, integers, text, logicals, NA and NULL, class(), and converting with as.numeric() and friends.

Runs in Zerethon IDE · R 4.5 Official R documentation ↗
main.R
count <- 3L        # integer
price <- 9.5       # numeric (double)
name <- "Ada"      # character
ready <- TRUE      # logical
nothing <- NULL

writeLines(paste(class(count), class(price), class(name), class(ready), is.null(nothing)))
print(as.integer("42") + 1L)
print(as.numeric("2.5") * 2)
writeLines(paste0(7, "7"))
print(c(7 / 2, 7 %/% 2, 7 %% 2, 2^10))
print(suppressWarnings(as.numeric("abc")))
print(c(1, NA, 3) * 2)

Output

integer numeric character logical TRUE
[1] 43
[1] 5
77
[1]    3.5    3.0    1.0 1024.0
[1] NA
[1]  2 NA  6
  • Numbers are doubles unless written with L (3L). / always divides exactly; %/% is integer division and %% the remainder.
  • NA is a missing value and spreads through calculations; NULL is “nothing at all”. Test them with is.na() and is.null(), never == NA.
  • Converting text that is not a number gives NA with the warning NAs introduced by coercion.
  • TRUE and FALSE are written in capitals.

Related topics

Official references