In every programming language there are many different variables are required to store data that you will be used within a program and those variables are assigned with a type and that variable has to assign that type of data only. In this article, we will cover the concept of data types in R programming language which include Vectors, Lists, Matrices, Arrays, factors and Data frames..

If you are looking for introduction to R, read this before proceeding ahead.

The elements of a data can be a numeric value, a character or a logical value.

** The data types in R are as follows:**

**Contents**hide

**Vectors**in R

It is a sequence of multiple data of same types, that is, numeric, character or logical. Few examples are shown below-

**Code & Output** given below

```
Age <-c(22,21,32,61,5) ## it is vector of numeric type.
Age
22 21 32 61 5
Names<-c("manvir","yash","alestaa","rohit","raj")
```

## it is a vector of character type consisting the name of five individuals

```
Names
"manvir" "yash" "alestaa" "rohit" "raj"
class(Age)
"numeric"
## this code will tell you the class of the elements in the vector, that is, they are character or numerical or logical.
class(Names)
"character"
```

**Matrices** in R

The second data types in R is matrix, a combination of multiple vectors with same types of elements. It is an object in which elements are arranged in a 2-D rectangular layout. In R, a matrix is created using the function** matrix()**.

**Arguments** for Matrices in R

`matrix(data, nrow, ncol, byrow = FALSE, dimnames = NULL)`

##Data will be the vector of same type elements of whose matrix we want to create.

## nrow and ncol will be tell R the number of row and column of the matrix.

## by default , in R the data will be arranged in matrix format column wise. If you want to arrange the data in the matrix row wise, set** byrow= TRUE.**

## suppose the matrix has two rows and two columns. The function dimnames is used to name the rows and columns of the matrix. By default, it is null and the matrix’s dimension has no names

Suppose you want to create a 1*3 matrix, that is, a matrix with 2 rows and three columns. The rows will show the amount you owe to three individuals – a, b, c. The amount is 5, 10 and 15 respectively.

**Code** for Matrices as data types in R

```
matrix(c(5,10,15), nrow=1, ncol=3, byrow= TRUE)
#Output
[,1] [,2] [,3]
[1,] 5 10 15
```

## we want the data to be row wise so byrow= TRUE (note that due to just one row , it does not makes sense)

## we can clearly see that since we did not specify the dimnames so they are nameless.

## now specifying the dimnames as well. It is of list data type.

**Code**

```
row<-c("amount") ## to specify the row name of the matrix
col<-c("a","b","c") ## to specify the column names of the matrix
matrix(c(5,10,15), nrow=1, ncol=3, byrow=TRUE,dimnames = list(row,col))
#Output
a b c
amount 5 10 15
```

**##**the same
matrix but with dimension names specified.

Similarly you can create matrix of other dimensions of your choice and name them accordingly.

**Lists** as data types in R

The third data types in R is lists, a sequence of different types of elements like numerics, characters and logical values and etc. Unlike a vector, different elements can be stored within a list without making them loose their originality.

**Code** example for Lists

```
L<-list("a","b",2, TRUE) ## we have stored 3 different types of elements- numeric, character and logical together.
L
#Output
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] 2
[[4]]
[1] TRUE
```

#### Example 2 for Lists in R

```
str(L) ## this code will tell us the type of each element in the list.
Output
List of 4
$ : chr "a"
$ : chr "b"
$ : num 2
$ : logi TRUE
## “chr” is character, “num” is numeric and “logi” is logical.
```

So we can see that we can store different type elements together in a list and elements do not lose their originality which is not possible in a vector.

**Arrays** as data types in R

The fourth data types in R is Array, these are similar to matrices but it can have more than two dimensions. To create an array, we call the array function and input a vector of values and a vector of dimensions. Unlike a matrix, it can have more than two dimensions. We also have the option of naming the dimensions.

**Arguments**

array(data = NA, dim = length(data), dimnames = NULL)

The data in an array will always be shown in column wise. So we will have to put the data accordingly.

**## data** – we will input the vector of the data whose array we want to create.

**## dim** – this argument will help us to input the dimensions of the arrays. It can have more than two dimensions.

**## dimnames** – this argument will help us to name the dimnesions of the array.

Suppose you want to create an array of marks in Mathematics and Physics of three students – a, b and c in class 10 and 12 so as to see their progress in each subject.

**Code** example for Arrays in r

```
students <-c("a","b","c")
subjects<-c("mathematics","physics")
standard<-c("class 10", "class 12")
## these vectors are created just to name the dimensions of the array
array(c(52,45,36,28,45,46,78,79,45,46,12,36),dim=c(3,2,2), dimnames=list(students,subjects,standard))
## this code will create an array of dimension
```

3(rows)*2(columns)*2(number of matrices) displaying the marks of the students in maths and physics in class 10 and 12 y. It will create 2 matrices of 3 rows and 2 columns.

**Output** for above code example for arrays in R

```
, , class 10
mathematics physics
a 52 28
b 45 45
c 36 46
, , class 12
mathematics physics
a 78 46
b 79 12
c 45 36
## Note that the data in the matrix gets fitted column wise by default so put the data accordingly.
```

**Factors** in R

Factors are the data objects which are used to categorize the data and store it as levels. They can store both integers and strings. Factors are created using the function **factor()**after taking a vector as an input.

**Code** example for Factors in R

```
data<-c("male","female","male","male","female","male")
##input vector
factor(data)
## this code will tell us the categories/ levels of the vector data.
```

**Output** of the above code example for factor

```
male female male male female male
Levels: female male
## the vector data contains six characters of two types – male and female.
```

**Data frame**s in R

A data frame in R is a table like structure in which each column contains values of one variable with “n” number of rows. The number of rows in each column should be equal otherwise this function will not work. Data frame is created using the function **data.frame().**

Suppose you want to create a table like structure for 5 people- a, b, c, d and e specifying their age and gender. You have all the required information with yourself.

**Code** example for Data frame in R

```
gender<-c("female","male","male","female","male")
name<-c("a","b","c","d","e")
age<-c(18,15,22,14,16)
data.frame(name,age,gender)
```

**Output** of the above code for Data Frame in R

```
name age gender
1 a 18 female
2 b 15 male
3 c 22 male
4 d 14 female
5 e 16 male
## you can use more variables well and add more columns and row to the table. Note that the number of elements in each variable should be same otherwise this function will not work.
```

Note that the number of elements in each variable should be same otherwise this function will not work.

With this, our introduction to R ends. Not to mention but , it is very helpful in your CV and many other tips are covered in How to prepare for an Actuarial Job?. We hope the article Data types in R will help you. Happy Learning. For our previous article introduction to R visit this link

Source: *MathematicaCity*