R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are assigned with R-objects and the R-object’s data type becomes the variable’s data type.

There are mainly 6 R-objects:

  • Vectors
  • Lists
  • Arrays
  • Matrices
  • Data frames
  • Factors

Vectors

Vector is a sequence of data elements of the same type. It is an one dimensional array. Simplest way to create a vector in R is to use the c() function.

Example:

a = c(1, 2, 4.2, -10, 3) #numeric vector

b = c(‘red’, ‘green’, ‘blue’)           #character vector

c = c(TRUE, TRUE, FALSE, TRUE) #logical vector

a

b

c
 

Note:  You can use = or <- to assign the values to a variable.

Output:

When we execute the above script file, it will display the following output:

[1]  1.0  2.0  4.2  -10.0  3.0

[1]   “red”    “green”     “blue”

[1]  TRUE   TRUE   FALSE    TRUE
 

There are five classes of vectors:

  • Numeric
  • Integer
  • Complex
  • Logical
  • Character
  • Numeric Data Types

Decimal values are used for the numeric data types. If you assign a numeric value for any variable x like given below, x will become a numeric type.

Example:

> x = 24.13    # assigns a decimal value to x

> x                 # Print the value of x

Output:

[1] 24.13

If we print the class name of x then:

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

Output will be:

[1] “numeric”

Furthermore, even if we assign an integer value to a variable y, it is still being saved as a numeric value.

> y = 100       # assigns a decimal value to y

> y                # Print the value of variable y

[1] 100

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

[1] “numeric”

  • Integer Data Types

To create an integer type of variable in R, you have to invoke the as.integer() function.

Example:

> y = as.integer(100)         # assigns a decimal value to y

> y #print the value of y

Output:

[1] 100

And if you check the class of y:

> class(y) #print the class of y

Output will be:

[1] “integer”

You can also append the L suffix to declare an integer.

Example:

> y = 100L

> class(y)

Output:

[1] “integer”

  • Complex data type

Complex value can be defined by the pure imaginary value ‘i’.

Example:

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

> k

Output:

[1] 1+2i

And if you check the class of k:

> class(k) #print the class of k

Output will be:

[1] “complex”

  • Logical Data Types

Logical value is mostly created via comparison variables.

Example:

> a = 13; b = 24 # assigning values

> c = a > b # is larger than b?

> c # print the logical value

Output:

[1] False

We can also check the class:

> v = TRUE

> class(v)

Output will be:

[1] “logical”

  • Character Data Types

Character object is used to enter or represent string values in R.  To convert objects into character, we have to use the as.character() function within your code.

Example:

> g = as.character(24.13)

> g #print the character string

Output:

[1] 3.14

And if we check the class of g:

> class(g) #print the class of g

Output will be:

[1] “character”

You can also assign some string values:

Example:

> s = “Hello World!” #assign string value

> s #print the value of s

Output:

Hello World!

Lists

List is a collection of R-objects which contains elements of different types like –strings, numbers, vectors and another list inside it.

Example:

> n = c(3, 1, 6)

> s = c("ab", "bc", "ca", "db", "ec")

> x = list(n, s, TRUE)

> x

Output:

[[1]]

[1] 3 1 6

[[2]]

[1] "ab" "bc" "ca" "db" "ec"

[[3]]

[1] TRUE

Matrices

Matrices are the R-objects, in which the two dimensional rectangular data set are arranged. A matrix can be built using the matrix() function.

Syntax: matrix(data, nrow, ncol, byrow, dimensions)

Where, data: - it is the input vector which will be the data elements of the matrix

nrow: -  number of rows to be created

ncol: -  number of columns to be created

byrow: - it is just a logical clue. If it is TRUE, then the input vector elements are arranged by row.

dimname: - it specifies the names assigned to the rows and columns.

Example 1:

> Mat <- matrix(c(1:9), nrow = 3, ncol = 3 )

> Mat

Output:

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

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

Example 2:

> Mat <- matrix(c(1:9), nrow = 3, ncol = 3, byrow = TRUE)

> Mat

Output:

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

[1,]    1    2    3

[2,]    4    5    6

[3,]    7    8    9

Arrays

Unlike matrices, arrays can be of any number of dimensions. It means they can store data in more than two dimensions. It also takes vectors as input. To create an array, it uses the dim parameter.

Example:

Let’s create an array with two elements with 3X3 matrices each.

> vector1 <- c(100,200,300)

> vector2 <- c(400,500,600,700,800,900)

> result <- array(c(vector1,vector2),dim = c(3,3,2))

> result

Output:

, , 1

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

[1,]  100  400  700

[2,]  200  500  800

[3,]  300  600  900

, , 2

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

[1,]  100  400  700

[2,]  200  500  800

[3,]  300  600  900

Factors

Factors are the R data objects. They are used to categorize the data and store it as levels. They can store both integers and strings. Factors are used in data analysis for statistical modeling. factor() function is used to create factors.

Example:

> data <- c('Physics','Maths','Physics','Bio','Bio','Maths','Physics','Maths','Bio')

> fdata <- factor(data)

> fdata

Output:

[1] Physics   Maths   Physics   Bio   Bio   Maths   Physics   Maths   Bio    

Levels: Bio Maths Physics

Data Frames

A data frame is a table or a two dimensional array like structure in which each column contains values of one variable (means the first column can be character, second column can be numeric and thirds column can be logical etc.) and each row has one set of values from each column. To create a data frame, we have to use data.frame() function.

> std_id = c (1:4)

> std_name = c('Nikita','Deep','Priya',"Pihu")

> marks = c(87, 89, 72, 65)

> std.data <- data.frame(std_id, std_name, marks)

> std.data
 

Output:

   std_id std_name marks

1      1      Nikita    87

2      2      Deep     89

3      3      Priya     72

4      4      Pihu     65
 

Related Topics

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

R Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.