4  Data Types in R

Practise this interactively in Course 1 (Introduction to R), lesson 4

Learning Objectives: After this chapter you will be able to:

  • Create numeric, integer, character, and logical values.
  • Check a value’s type with is.numeric(), is.integer(), is.character(), and is.logical().
  • Convert between types with as.integer(), as.character(), and friends.
  • Know where to find implicit coercion of mixed types in the next chapter.
  • Create and format a date, and recognize POSIXct for date-times.

Every value in R has a type, and the type decides what you can do with it. This chapter walks through the five you will meet first: numeric (double), integer, character, logical, and date/time. Most of the time you will let R infer the type for you, and then use the is.*() and as.*() functions to check or change it.

4.1 Numeric

In R, we represent numbers with the data type numeric. We will first create a variable and assign it a value. Next we will learn how to check the type of the variable.

# create two variables
number1 <- 3.5
number2 <- 3

# check data type
class(number1)
[1] "numeric"
class(number2)
[1] "numeric"
# check if data type is numeric
is.numeric(number1)
[1] TRUE
is.numeric(number2)
[1] TRUE

In the example above, R also treats integers as numeric/double. We will learn to create integers in a while. We used two new functions in the example above:

  • class(): returns the class or type
  • is.numeric(): tests whether the variable is of type numeric

4.2 Integer

Unless specified otherwise, integers are treated as numeric or double. Let us create variables of the type integer and convert other data types to integer.

  • create a variable number1 and assign it the value 3
  • check the data type of number1 using class
  • create a second variable number2 using as.integer and assign it the value 3
  • check the data type of number2 using class
  • finally use is.integer to check the data type of both number1 and number2
# create a variable and assign it an integer value
number1 <- 3

# create another variable using as.integer
number2 <- as.integer(3)

# check the data type
class(number1)
[1] "numeric"
class(number2)
[1] "integer"
# use is.integer to check data type
is.integer(number1)
[1] FALSE
is.integer(number2)
[1] TRUE

4.3 Character

We represent letters, words and groups of words with the data type character. We enclose data of type character in single or double quotes. R treats any value enclosed in quotes as character. Let us create two variables to store a first and last name.

# first name
first_name <- "jovial"

# last name
last_name <- 'mann'

# check data type
class(first_name)
[1] "character"
class(last_name)
[1] "character"
# use is.character to check data type
is.character(first_name)
[1] TRUE
is.character(last_name)
[1] TRUE

You can coerce any data type to character using as.character().

# create variable of different data types
age <- as.integer(30) # integer
score <- 9.8          # numeric/double
opt_course <- TRUE    # logical
today <- Sys.time()   # date time

as.character(age) 
[1] "30"
as.character(score)
[1] "9.8"
as.character(opt_course)
[1] "TRUE"
as.character(today)
[1] "2026-11-30 05:16:05.022079"

4.4 Logical

Logical data types take only 2 values. Either TRUE or FALSE (plus NA for missing values, giving three-valued logic in R). We create such data types when we compare two objects in R using comparison or logical operators.

  • create two variables x and y
  • assign them the values TRUE and FALSE respectively
  • use is.logical to check data type
  • use as.logical to coerce other data types to logical
# create variables x and y
x <- TRUE
y <- FALSE

# check data type
class(x)
[1] "logical"
is.logical(y)
[1] TRUE

The outcome of comparison operators is always logical. In the example below, we compare two numbers to see the outcome.

# create two numeric variables
x <- 3
y <- 4

# compare x and y
x > y
[1] FALSE
x < y
[1] TRUE
# store the result
z <- x > y
class(z)
[1] "logical"

TRUE is represented by all numbers except 0. FALSE is represented only by 0.

# TRUE and FALSE are represented by 1 and 0
as.logical(1)
[1] TRUE
as.logical(0)
[1] FALSE
# using numbers
as.numeric(TRUE)
[1] 1
as.numeric(FALSE)
[1] 0
# using different numbers
as.logical(c(-2, -1.5, -1, 0, 1, 2))
[1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE

Use as.logical() to coerce other data types to logical.

# create variable of different data types
age <- as.integer(30) # integer
score <- 9.8          # numeric/double
opt_course <- TRUE    # logical
today <- Sys.time()   # date time

as.logical(age) 
[1] TRUE
as.logical(score)
[1] TRUE
as.logical(opt_course)
[1] TRUE

4.5 Date

R has dedicated types for dates and date-times. Use Date for calendar dates and POSIXct for date-times:

# today's date
today_date <- Sys.Date()
today_date
[1] "2026-11-30"
class(today_date)
[1] "Date"
# create a date from a string
launch_date <- as.Date("2026-03-01")
launch_date
[1] "2026-03-01"
# pretty-print a date
format(launch_date, "%B %d, %Y")
[1] "March 01, 2026"

For date-times, use as.POSIXct(). For arithmetic with dates, such as finding differences, adding months or handling time zones, use the lubridate package.

Key points

  • Numeric, integer, character and logical values support different operations.
  • Use class() and the is.*() functions to inspect a value.
  • Use as.*() functions to convert values between types.
  • Use Date for calendar dates and POSIXct for date-times.

Next: Vectors in R

Practise interactively: Course 1, lesson 4