R Data Types

[This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A data type of a variable specifies the type of data that is stored inside that variable. 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 type of x


[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

bool2 <- FALSE


[1] TRUE
[1] "logical"
[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

# real numbers
height <- 182


[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


[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


[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"

# create a character variable
my_char <- 'A'


[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")


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



 [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.

To leave a comment for the author, please follow the link and comment on their blog: R feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)