Programs

Top 5 R Data Types | R Data Types You Should Know About

Certain variables are needed to store the data that you are using within the program to create any application or render any programming in any programming language. One may also note (usually in typical or most programming languages such as C or C++) that these variables are assigned to specific categories. These categories are what we refer to as the data type.

Data types are a very important concept available in almost all the programming languages. As the name indicates, a data type represents a specific kind of data which can be processed using your computer program. Learn about the various data types of Python.

In contrast to other programming languages such as C, the variables are not simply declared as some R data type, but assigned with R objects. The data type of the R object becomes the data type of the variable. There are several types of R objects most common being:

  1. Vectors
  2. Matrices
  3. Lists
  4. Arrays
  5. Factors
  6. Data Frames

Vectors are the most basic R data types holding elements of different classes. There are five major data types of these atomic vectors. The other R-Objects are built upon the atomic vectors.

Mentioned below are some of the R data types used in creating vectors:

Top R Data Types

1. Numeric Data Type

In R programming language, decimal values are called numerics. It is the default R data type assigned to all values in R. Let us understand it with the help of an example:

> y = 11.6 # assigns a decimal value to y variable

> y # prints the value of y

[1] 11.6

> class(y) # prints the class name of y 

[1] “numeric”

Here, decimal value (11.6) has been assigned to a variable “y”, whose data type is numeric by default.

Even if you give the variable a non-decimal value, its default data type will still be numeric and not an integer. Here is an example for you:

> y = 1 

> y # print the value of y variable

[1] 1 

> class(y) # print the class name of y

[1] “numeric”

Read: Variables and Data Types in Python [An Ultimate Guide for Developers]

2. Integer Data Type

Numbers without the decimal values are declared under the data type integer in R programming language. To create an integer variable in R, the integer function is revoked. Also, is.integer function can be applied to ensure that y is indeed an integer. Let us look at a few examples to understand the integer data type:

> x = as.integer(5) 

> x # print the value of x 

[1] 5

> class(x)       

[1] “integer” 

> is.integer(x) # function to ensure if x is an integer or not

[1] TRUE

Another way to declare an integer is by appending an L suffix.

> x = 5L 

> is.integer(x) # function to ensure if x is an integer or not 

[1] TRUE

A value with numeric data type can be coerced into an integer data type with as.integer function.

> as.integer(7.16) # coercing a numeric value 

[1] 7

A string with decimal values can also be parsed using the as.integer function.

> as.integer(“7.16”) # coercing a decimal string 

[1] 7

However, it would be wrong to parse a non-decimal string to the integer function. 

Checkout: MATLAB Data Types

3. Complex Data Type 

A complex value in the R programming language is defined via the pure imaginary value i.

> k = 1 + 2i # creating a complex number 

> k            

[1] 1+2i 

> class(k)     

[1] “complex”

4. Logical Data Type

The logical data types in R take either true or false value. This value is generated after comparing the two values. Mentioned below is an example for you:

> l = 4; m = 2   

> n = l > m # is l larger than y? 

> n # printing t the logical value 

[1] TRUE 

> class(n) # printing the class name of z 

[1] “logical”

5. Character Data Type 

A character data type is used to represent string values in the R Programming language. Objects are converted into character values using the as.character () function. Mentioned below are some example to built a clear understanding of character data type: 

> y = as.character(7.16) 

> y # print the character string 

[1] “7.16” 

> class(y) # print the class name of y 

[1] “character”

To concatenate two character values, a paste function can be used. 

> fname = “Riya”; lname =”Sharma” 

> paste(fname, lname) 

[1] “Riya Sharma”

Now, since you have developed an understanding of the most common data types, let us look at how we can create vector objects and use the data types to declare values. 

A vector is a set of data elements of the same type of data. 

Given below is a vector containing three numeric values 4, 5, and 6.

> c(4, 5, 6) 

[1] 4 5 6

And here is a vector of logical values.

> c(FALSE TRUE FALSE TRUE) 

[1] FALSE TRUE FALSE TRUE

A vector can also contain character strings.

> c(“AA”, “BB”, “CC”, “DD”, “EE”) 

[1] “AA” “BB” “CC” “DD” “EE”

Other R Objects

Matrices

Creates a two-dimensional data set. Here is an example: 

Create a matrix.

M = matrix( c(‘q’,’w’,’e’,’r’,’t’,’y’), nrow = 2, ncol = 3, byrow = TRUE)

print(M)

Output –  

     [,1] [,2] [,3]

[1,] “q” “w” “e” 

[2,] “r” “t” “y”

Lists

A list is a type of object containing different types of elements: vectors, functions, and even other lists.

Example

#Create a list containing vectors and numeric values.

list1 <- list(c(2,5,3),7.9)

# Print the list.

print(list1)

Output – 

[[1]]

[1] 2 5 3

[[2]]

[1] 7.9

Arrays

Unlike matrices, arrays can be of n dimensions. The dim attribute used in arrays creates the required number of dimensions. Mentioned below is an example to create an array with two elements with 2X2 matrices.

 Create an array.

a <- array(c(‘pink’,’blue’),dim = c(2,2,1))

print(a)

When we execute the above code, it produces the following result −

, , 1

     [,1] [,2]    

[1,] “pink” “blue” 

[2,] “blue” “pink”  

Factors

Factors are r-objects created using a vector. Factors store the vectors with distinct values of elements in the vector as labels. The labels always have character data type irrespective of data type in the input vector. Factors are extremely useful in carrying out statistical modelling.

Factors can be created using the factor() function. 

# Create a vector.

colors <- c(‘yellow’,’blue’,’pink’)

# Create a factor object.

factor_colors <- factor(colors)

# Print the factor.

print(factor_colors)

[1] yellow blue pink

Data Frames

Data frames are tabular data objects. Each column in a data frame can contain different modes of data. All three columns can have different data types, may it be an integer, numeric, or character. 

Data Frames are created using the data.frame() function.

# Create the data frame.

Data <- data.frame(

   gender = c(“Male”, “Female”), 

   height = c(153, 160), 

   weight = c(80, 78),

   Age = c(40 29)

)

print(BMI)

Output

  gender height weight Age

1 Male 153.0 80 40

2 Female 160.0 78 29  

Also Read: R Developer Salary in India: For Freshers & Experienced

What Next?

If you are curious to learn about R, data science, check out our PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Prepare for a Career of the Future

PG DIPLOMA FROM IIIT-B, 100+ HRS OF CLASSROOM LEARNING, 400+ HRS OF ONLINE LEARNING & 360 DEGREES CAREER SUPPORT
Enroll Today

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Data Science Course

×