Table of contents
1.
Introduction
2.
Data Types in R
2.1.
Logical
2.2.
Integer
2.3.
Double
2.4.
Complex Number
2.5.
String
3.
Frequently Asked Questions
3.1.
How can I convert a numeric value to a character string in R?
3.2.
How can I check the data type of a variable in R?
3.3.
Are there any restrictions on the length of character strings in R?
3.4.
Can I perform mathematical operations on character strings in R?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to R Data Types

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

R is a popular computer language for statistical computation and data analysis. It is a popular choice among professionals due to its numerous advantages. R enables users to analyse and visualise data effectively. It is because of its great support for statistical processes and image production. R is a free and open-source programming language. Anyone interested in data science should look at R. It is adaptable, can communicate with other tools, and is widely used in academics and industry.

Introduction to R Data Types

In this article, we will have an Introduction to R Data Types.

Data Types in R

So, What is data type? Well, just like there are different genres in movies. Data too are categorised in different ways too. In the  R Programming language, data objects are assigned specific data types to represent different kinds of information. In this comprehensive guide, we will delve into five important data types in R, progressing from the simplest to the most complex; logicalintegerdoublecomplex numbers, and strings. By understanding these data types, you will gain a solid foundation for working with diverse data in R.

Logical

Logical data type or Boolean data type, In R, there isn’t exactly a difference between them. The logical data type is the most basic in R, considering it has only two values: TRUE or FALSE. Logical values are commonly encountered when performing comparisons or running tests in R.

Given below are various examples of Logical data types. 

Example:  

# Logical example
result <- 5 >= 3
print(result)
# Output: TRUE

values <- c(1, 2, 3, 4, 5) >= c(5, 4, 3, 2, 1)
print(values)
# Output: FALSE FALSE TRUE TRUE TRUE

lazy_logical <- T
print(lazy_logical)
# Output: TRUE

lazy_logical <- F
print(lazy_logical)
# Output: FALSE

Integer

Integers are denoted by a number followed by the letter "L." Integers do not have any decimals. The integer data type represents whole numbers in R. Unlike other programming languages, R explicitly distinguishes integers from floating-point numbers (doubles). 

Given below are various examples of Integer data types.

Example:

# Integer example
not_integer <- typeof(1)
print(not_integer)
# Output: "double"

integer_value <- typeof(1L)
print(integer_value)
# Output: "integer"

result <- 1L + 1.5
print(result)
# Output: 2.5

Double

The double data type is short for a double-precision number. It is the primary numeric data type in R. Doubles are used to represent real numbers with decimal places and can handle a wide range of numeric values.

Given below are various examples of Double data types.

Example:

# Double example
result <- typeof(1)
print(result)
# Output: "double"

result <- typeof(3.4)
print(result)
# Output: "double"

result <- typeof(-3e15)
print(result)
# Output: "double"

result <- typeof(1/3)
print(result)
# Output: "double"

Complex Number

The R language has separate data types for complex numbers, unlike other languages. Because R is a language that is focused on statistics and data, a complex number is required. R supports complex numbers, which consist of a real part and an imaginary part. Complex numbers are represented as “<real> + <imaginary>i” in R. Although less frequently used, they are essential for certain mathematical computations.

Given below are various examples of Complex Number data types.

Example:

# Complex number example
result <- sqrt(-1)
print(result)
# Output: NaN (Not a Number)

result <- sqrt(-1 + 0i)
print(result)
# Output: 0 + 1i

String

The string data type in R is also interchangeably used with the character data type. It is used to handle textual data in R. Strings are enclosed in either single quotes (') or double quotes ("). They can contain any character or sequence of characters.

Given below are various examples of String data types.

Example:

# String example
letters <- "abcdefg"
print(letters)
# Output: "abcdefg"

numbers <- '12345678'
print(numbers)
# Output: '12345678'

sentence <- "string of words"
print(sentence)
# Output: "string of words"

Frequently Asked Questions

How can I convert a numeric value to a character string in R?

You can use the inbuilt function as.character() function to convert a numerical value to a character string. You just need to pass a numeric value as an argument. For example, as.character(123) will convert the numeric value 123 to the character string "123".

How can I check the data type of a variable in R?

You can use the typeof() function in R to check the data type of a variable, as shown in the examples above. For example, typeof(x) will return the data type of the variable x, such as "numeric", "character", or "logical".

Are there any restrictions on the length of character strings in R?

In R, there are restrictions on the length of the character strings in R. It can be as long as you want based on your needs. It is just that character strings with long lengths will take up more space in your memory.

Can I perform mathematical operations on character strings in R?

Direct mathematical operations on character strings are impossible in R. If you attempt to perform arithmetic operations like adding or subtracting character strings, you will encounter an error. Character strings are used for textual data, not numerical calculations.

Conclusion

In this article, we had a brief Introduction to R data types. We looked at various types of data types in R with examples. We looked at 5 major data types in R; logical, Integer, Double, Complex Number, and String. In the end, we also discussed some of the most frequently asked questions regarding R data types.

Recommended Readings:


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass