top of page

R Data Types

A variable can store different types of values such as numbers, characters etc. These different types of data that we can use in our code are called data types. For example,

x <- 123L

Here, 123L is an integer data. So the data type of the variable x is integer.

We can verify this by printing the class of x.


x <- 123L

# print value of x
print(x)

# print type of x
print(class(x))

Output

[1] 123
[1] "integer"

Here, x is a variable of data type integer.


Different Types of Data Types

In R, there are 6 basic data types:

  • logical

  • numeric

  • integer

  • complex

  • character

  • raw

Let's discuss each of these R data types one by one.


1. Logical Data Type


The logical data type in R is also known as boolean data type. It can only have two values: TRUE and FALSE. For example,

bool1 <- TRUE
print(bool1)
print(class(bool1))
bool2 <- FALSE
print(bool2)
print(class(bool2))

Output

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

In the above example,


bool1 has the value TRUE,
bool2 has the value FALSE.

Here, we get "logical" when we check the type of both variables.


Note: You can also define logical variables with a single letter - T for TRUE or F for FALSE. 
For example,

is_weekend <- F 
print(class(is_weekend))  
# "logical"

2. Numeric Data Type

In R, the numeric data type represents all real numbers with or without decimal values. For example,

# floating point values
weight <- 63.5
print(weight)
print(class(weight))

# real numbers
height <- 182
print(height)
print(class(height))

Output

[1] 63.5
[1] "numeric"
[1] 182
[1] "numeric"

Here, both weight and height are variables of numeric type.


3. Integer Data Type


The integer data type specifies real values without decimal points. We use the suffix L to specify integer data. For example,


integer_variable <- 186L
print(class(integer_variable))

Output

[1] "integer"

Here, 186L is an integer data. So we get "integer" when we print the class of integer_variable.


4. Complex Data Type


The complex data type is used to specify purely imaginary values in R. We use the suffix i to specify the imaginary part. For example,


# 2i represents imaginary part
complex_value <- 3 + 2i

# print class of complex_value
print(class(complex_value))

Output

[1] "complex"

Here, 3 + 2i is of complex data type because it has an imaginary part 2i.


5. Character Data Type


The character data type is used to specify character or string values in a variable.

In programming, a string is a set of characters. For example, 'A' is a single character and "Apple" is a string.

You can use single quotes '' or double quotes "" to represent strings. In general, we use:

  • '' for character variables

  • "" for string variables

For example,


# create a string variable
fruit <- "Apple"
print(class(fruit))

# create a character variable
my_char <- 'A'
print(class(my_char))

Output

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

Here, both the variables - fruit and my_char - are of character data type.


6. Raw Data Type

A raw data type specifies values as raw bytes. You can use the following methods to convert character data types to a raw data type and vice-versa:

  • charToRaw() - converts character data to raw data

  • rawToChar() - converts raw data to character data

For example,


# convert character to raw
raw_variable <- charToRaw("Welcome to Programiz")

print(raw_variable)
print(class(raw_variable))

# convert raw to character
char_variable <- rawToChar(raw_variable)

print(char_variable)
print(class(char_variable))

Output

[1] 57 65 6c 63 6f 6d 65 20 74 6f 20 50 72 6f 67 72 61 6d 69 7a
[1] "raw"
[1] "Welcome to Programiz"
[1] "character"

In this program,

  • We have first used the charToRaw() function to convert the string "Welcome to Programiz" to raw bytes. This is why we get "raw" as output when we print the class of raw_variable.

  • Then, we have used the rawToChar() function to convert the data in raw_variable back to character form. This is why we get "character" as output when we print the class of char_variable.

Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Comments


bottom of page