# 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
Free interactive courses with business data, in your browser.
library(dplyr) read.csv("orders.csv") |> count(payment_method) |> slice_max(n, n = 3)
payment_method n 1 UPI 4967 2 Credit Card 2487 3 Debit Card 1282
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(), andis.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
POSIXctfor 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.
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.
[1] "numeric"
[1] "numeric"
[1] TRUE
[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 typeis.numeric(): tests whether the variable is of type numericUnless specified otherwise, integers are treated as numeric or double. Let us create variables of the type integer and convert other data types to integer.
number1 and assign it the value 3number1 using classnumber2 using as.integer and assign it the value 3number2 using classis.integer to check the data type of both number1 and number2We 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"
[1] "character"
[1] TRUE
[1] TRUE
You can coerce any data type to character using as.character().
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.
x and yTRUE and FALSE respectivelyis.logical to check data typeas.logical to coerce other data types to logical[1] "logical"
[1] TRUE
The outcome of comparison operators is always logical. In the example below, we compare two numbers to see the outcome.
[1] FALSE
[1] TRUE
[1] "logical"
TRUE is represented by all numbers except 0. FALSE is represented only by 0.
[1] TRUE
[1] FALSE
[1] 1
[1] 0
[1] TRUE TRUE TRUE FALSE TRUE TRUE
Use as.logical() to coerce other data types to logical.
R has dedicated types for dates and date-times. Use Date for calendar dates and POSIXct for date-times:
[1] "2026-11-30"
[1] "Date"
[1] "2026-03-01"
[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.
class() and the is.*() functions to inspect a value.as.*() functions to convert values between types.Date for calendar dates and POSIXct for date-times.Next: Vectors in R
Practise interactively: Course 1, lesson 4
Free interactive courses with business data, in your browser.
library(dplyr) read.csv("orders.csv") |> count(payment_method) |> slice_max(n, n = 3)
payment_method n 1 UPI 4967 2 Credit Card 2487 3 Debit Card 1282
Free interactive courses with business data, in your browser.
library(dplyr) read.csv("orders.csv") |> count(payment_method) |> slice_max(n, n = 3)
payment_method n 1 UPI 4967 2 Credit Card 2487 3 Debit Card 1282