R Data Types

Spread the love

In this tutorial, you will learn about various data types in R programming language.

Data Types in R Programming –

To do programming in R, It’s important to understand how the values and data types works in R. Let’s start with literal values.

A literal value is part of a statement or expression. For instance, the numbers 170 and 5 are literal values because they literally represent the numbers 170 and 5. Every literal value has a type.

In R, there are five basic data types –

  1. Logical
  2. Numeric
  3. Integer
  4. Character
  5. Complex

1 . Logical –

The logical data type in R is also known as boolean data type. It can only have two values: TRUE and FALSE or simply T and F. All the letters are in UPPERCASE, because R is case-sensitive it differentiates between uppercase and lowercase.

In programming languages, the logical literals represent two values: 1 (for true) and 0 (for false). Similarly, R also counts the TRUE literal as 1 and the FALSE literal as 0.

For example, if we write

> bool1 <- TRUE
> print(bool1)
[1] TRUE
> print(class(bool1))
[1] "logical"
> 
> bool2 <- FALSE
> print(bool2)
[1] FALSE
> print(class(bool2))
[1] "logical"
> 

The value of bool1 is TRUE and the value of bool2 is FALSE. And if we check the type of it, we can see that it says logical.

2. Numeric –

There are two types of numbers in R: integers (Integer) and decimal/real (Numeric) numbers.

170, 5, and 125 are integer numbers (i.e. whole numbers). 0.85, 3.14, and 0.0005 are decimal numbers. Note that the period symbol (.) separates the integer from the decimal in decimal numbers.

For example, if we write

> num1 <- 0.05
> print(class(num1))
[1] "numeric"
> 

The value of num1 is 0.05 and the data types is numeric.

3. Integer –

we learned that R considers any number (integer or decimal) a Numeric literal. So, what if we want to represent a whole number as an Integer?

To force R to recognize numbers as integer literals (without the decimal part), the letter L must accompany the number.

For example, we can write

> num2 <- 125L
> print(num2)
[1] 125
> print(class(num2))
[1] "integer"

4. Character –

We’ve established that R uses symbols, letters, or words (T, F, L, e, E, TRUE, FALSE) to accomplish tasks. However, sometimes we want to use words and characters as they are without any interpretation from R. We call these Character literals.

We represent character literals by enclosing them in quotation marks (") or apostrophes ('). Quotation marks or apostrophes allow us to distinguish character values from other literal values (Integer, Logical, and Numeric). Here are some examples of character literals: "Hello", "True", "FALSE", "e", "six", "12.4", and "Good Morning Folks ;-)".

Mixing quotation marks and apostrophes in the same character value is not valid syntax. For example, “Hello” won’t represent a character word if we have typed "Hello' or 'Hello"

Note that if we use “FALSE” (with quotation marks), R doesn’t consider it a logical literal; rather, it considers it a character literal. Similarly, "12.4" isn’t a numeric literal but a character literal instead.

> word <- "Hello, world"
> print(word)
[1] "Hello, world"
> print(class(word))
[1] "character"

5. Complex –

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,

> complex_val <- 5 + 3i
> print(complex_val)
[1] 5+3i
> print(class(complex_val))
[1] "complex"

Rating: 1 out of 5.

Posted in RTagged

Leave a Reply