RData Types (original) (raw)

R-Data Types

Last Updated : 05 Jun, 2025

Data types in R define the kind of values that variables can hold. Choosing the right data type helps optimize memory usage and computation. Unlike some languages, R does not require explicit data type declarations while variables can change their type dynamically during execution.

R Programming language has the following basic R-data types and the following table shows the data type and the values that each data type can take.

Basic Data Types Values Examples
Numeric Set of all real numbers "numeric_value <- 3.14"
Integer Set of all integers, Z "integer_value <- 42L"
Logical TRUE and FALSE "logical_value <- TRUE"
Complex Set of complex numbers "complex_value <- 1 + 2i"
Character "a", "b", "c", ..., "@", "#", "$", ...., "1", "2", ...etc "character_value <- "Hello Geeks"
raw as.raw() "single_raw <- as.raw(255)"

1. Numeric Data type in R

Decimal values are called numeric in R. It is the default R data type for numbers in R. If we assign a decimal value to a variable x as follows, x will be of numeric type.
Real numbers with a decimal point are represented using this data type in R. It uses a format for double-precision floating-point numbers to represent numerical values.

R `

x = 5.6

print(class(x))

print(typeof(x))

`

Output

[1] "numeric" [1] "double"

Even if an integer is assigned to a variable y, it is still saved as a numeric value.

R `

y = 5

print(class(y))

print(typeof(y))

`

Output

[1] "numeric" [1] "double"

When R stores a number in a variable, it converts the number into a "double" value or a decimal type with at least two decimal places.
This means that a value such as "5" here, is stored as 5.00 with a type of double and a class of numeric. And also y is not an integer here can be confirmed with the **is.integer() function.

R `

y = 5

print(is.integer(y))

`

2. Integer Data type in R

R supports integer data types which are the set of all integers. we can create as well as convert a value into an integer type using the **as.integer() function.
we can also use the capital 'L' notation as a suffix to denote that a particular value is of the integer R data type.

R `

x = as.integer(5)

print(class(x))

print(typeof(x))

y = 5L

print(class(y))

print(typeof(y))

`

Output

[1] "integer" [1] "integer" [1] "integer" [1] "integer"

3. Logical Data type in R

R has logical data types that take either a value of **true or **false. A logical value is often created via a comparison between variables.
Boolean values, which have two possible values, are represented by this R data type: FALSE or TRUE

R `

x = 4 y = 3

z = x > y

print(z)

print(class(z))

print(typeof(z))

`

Output

[1] TRUE [1] "logical" [1] "logical"

4. Complex Data type in R

R supports complex data types that are set of all the complex numbers. The complex data type is to store numbers with an imaginary component.

R `

x = 4 + 3i

print(class(x))

print(typeof(x))

`

Output

[1] "complex" [1] "complex"

5. Character Data type in R

R supports character data types where we have all the alphabets and special characters. It stores character values or strings. Strings in R can contain alphabets, numbers, and symbols.
The easiest way to denote that a value is of character type in R data type is to wrap the value inside single or double inverted commas.

R `

char = "Geeksforgeeks"

print(class(char))

print(typeof(char))

`

Output

[1] "character" [1] "character"

There are several tasks that can be done using R data types. Let's understand each task with its action and the syntax for doing the task along with an R code to illustrate the task.

**6. Raw data type in R

To save and work with data at the byte level in R, use the raw data type. By displaying a series of unprocessed bytes, it enables low-level operations on binary data. Here are some speculative data on R's raw data types:

R `

x <- as.raw(c(0x1, 0x2, 0x3, 0x4, 0x5)) print(x)

`

Five elements make up this raw vector x, each of which represents a raw byte value.

Find Data Type of an Object in R

To find the data type of an object we have to use **class() function. The syntax for doing that is we need to pass the object as an argument to the function **class() to find the data type of an object.

**Syntax

class(object)

**Example

R `

print(class(TRUE))

print(class(3L))

print(class(10.5))

print(class(1+2i))

print(class("12-04-2020"))

`

Output

[1] "logical" [1] "integer" [1] "numeric" [1] "complex" [1] "character"

Type verification

We can verify the data type of an object, if we doubt about it's data type. To do that, we need to use the prefix "is." before the data type as a command.

**Syntax

is.data_type(object)

**Example

R `

print(is.logical(TRUE))

print(is.integer(3L))

print(is.numeric(10.5))

print(is.complex(1+2i))

print(is.character("12-04-2020"))

print(is.integer("a"))

print(is.numeric(2+3i))

`

Output

[1] TRUE [1] TRUE [1] TRUE [1] TRUE [1] TRUE [1] FALSE [1] FALSE

Coerce or Convert the Data Type of an Object to Another

The process of altering the data type of an object to another type is referred to as coercion or data type conversion. This is a common operation in many programming languages that is used to alter data and perform various computations. When coercion is required, the language normally performs it automatically, whereas conversion is performed directly by the programmer.

Coercion can manifest itself in a variety of ways, depending on the R programming language and the context in which it is employed. In some circumstances, the coercion is implicit, which means that the language will change one type to another without the programmer having to expressly request it.

**Syntax

as.data_type(object)

**Note: All the coercions are not possible and if attempted will be returning an "NA" value.

**For Detailed Explanation - **Data Type Conversion in R

**Example

R `

print(as.numeric(TRUE))

print(as.complex(3L))

print(as.logical(10.5))

print(as.character(1+2i))

print(as.numeric("12-04-2020"))

`

**Output

[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by coercion

In this article we learned about different R data types and what kind of data do they hold.