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. NAis a missing value and spreads through calculations;NULLis “nothing at all”. Test them withis.na()andis.null(), never== NA.- Converting text that is not a number gives
NAwith the warningNAs introduced by coercion. TRUEandFALSEare written in capitals.