Introduction

As a powerful statistical programming language, R has a wide variety of data types and data structures. To be proficient in R, it is important to understand these data types and learn how to work with them.

In this guide, you will learn concepts and techniques for working with data types in R.

R works with several data types. Some of the basic ones include:

**Characters**: Text (or string) values are called characters, as shown in the example below. If we want to confirm its type, we can use the*class()*function. In fact, R provides many functions like*class()*that can be used to examine features of objects, such as*typeof(), length(),*and*attributes()*.

```
1t = "data types"
2class(t)
```

{r}

Output:

`1[1] "character"`

**Numeric**: Decimal values like 2.3 is called numeric in R. It is the default computational data type.

```
1N = 2.3
2class(N)
3
```

{r}

Output:

`1[1] "numeric"`

Note that the variable 'N' is stored as a numeric value and not as an integer. This can be checked using the *is.integer()* function, as shown below:

`1is.integer(N)`

{r}

Output:

`1[1] FALSE`

**Integers**: If we want to create an integer variable, we can use the*integer*function. Also, all integers are numeric, but the reverse is not true.

```
1i = as.integer(3)
2is.integer(i)
3
4is.numeric(i)
```

{r}

Output:

```
1[1] TRUE
2
3[1] TRUE
```

**Logical**: Logical values are often created by comparing two or more variables. These are denoted by Boolean values,*TRUE*or*FALSE*.

```
1x = 100
2y = 56
3x < y
```

{r}

Output:

`1[1] FALSE `

: The complex variable is defined by the imaginary value*Complex**i*.

```
1z = 3 + 2i
2class(z)
```

{r}

Output:

`1[1] "complex"`

The above examples are the basic data types in R. However, this is not an exhaustive list of classes. R also has many data structures, as discussed in the subsequent sections.

A vector is the most common data structure in R. It is a sequence of elements of the same data type. The *vector()* function can be used to create a vector. The default mode is logical, but we can use constructors such as *character(), numeric(),* etc., to create a vector of a specific type.

The lines of code below construct a numeric and a logical vector, respectively. A vector can also contain strings, as indicated by the vector 's'.

```
1n <- c(1,2,5.3,6,-2,4)
2l <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE)
3s = c("USA", "UK", "AFRICA", "INDIA", "CHINA")
4
5class(n)
6class(l)
7class(s)
```

{r}

Output:

```
1[1] "numeric"
2
3[1] "list"
4
5[1] "character"
```

It is also possible to perform several operations on the vectors, such as combining vectors and vector mathematics. For example, the *first line of code* below combines the vectors 'n' and 'l', while the *second line* prints the elements of the new vector. You can check the length of the resultant vector using the *length* function, as shown in the *third command*. The *fourth line* checks the data type, and the resulting vector is of 'character' type. This is called value coercion in vector combinations.

```
1comb = c(n, s)
2comb
3
4length(comb)
5class(comb
```

{r}

Output:

```
1[1] "1" "2" "5.3" "6" "-2" "4" "USA" "UK"
2 [9] "AFRICA" "INDIA" "CHINA"
3
4
5[1] 11
6
7
8[1] "character"
```

It is also possible to perform mathematical operations on vectors, as in the lines of code below.

```
1x = c(5, 3, 4)
2y = c(1, 2, 3)
3
4#Arithmetic Operations
55 * x
6x-y
7x+y
8x/y
```

{r}

Output:

```
1[1] 25 15 20
2
3[1] 4 1 1
4
5[1] 6 5 7
6
7[1] 5.000000 1.500000 1.333333
```

In R, matrices are an extension of numeric or character vectors. All columns in a matrix must have the same mode and the same length. Also, similar to atomic vectors, the elements of a matrix must be of the same data type. The general representation of a matrix is shown in the line of code below.

The arguments *nrow* and *ncol* denote the number of rows and columns, respectively. The argument *byrow = TRUE* indicates that the matrix should be filled by the rows.

```
1m = matrix(c(20, 45, 33, 19, 52, 37), nrow=2, ncol=3, byrow = TRUE)
2print(m)
```

{r}

Output:

```
1 [,1] [,2] [,3]
2[1,] 20 45 33
3[2,] 19 52 37
```

It is possible to identify the rows, columns, or elements of a matrix using subscripts. For example, the element at the second row and second column can be accessed using the following command.

`1m[2, 2]`

{r}

Output:

`1[1] 52`

A list is a generic vector containing a collection of objects (or components). The advantage of a list is that it allows you to store a variety of objects that may be unrelated under one name.

The lines of code below create a list containing three vectors: name, place, and age in years.

```
1name = c("abhi", "ansh", "ajay")
2place = c("delhi", "mumbai", "pune")
3age = c(TRUE, FALSE, TRUE, FALSE, FALSE)
4
5l = list(name, place, age)
6print(l)
```

{r}

Output:

```
1[[1]]
2[1] "abhi" "ansh" "ajay"
3
4[[2]]
5[1] "delhi" "mumbai" "pune"
6
7[[3]]
8[1] TRUE FALSE TRUE FALSE FALSE
```

```
1l[2]
2
3l[c(2, 3)]
```

{r}

Output:

```
1[[1]]
2[1] "delhi" "mumbai" "pune"
3
4[[2]]
5[1] TRUE FALSE TRUE FALSE FALSE
```

Data frame is perhaps the most important data type in R. It is in fact the de-facto data structure for most tabular data and is used extensively in data science. In simple terms, it is a special type of list where all the elements are of equal length.

Data frames are often imported into R using the *read.csv()* and *read.table()* functions. You can also create a new data frame with the *data.frame()* function, as in the line of code below.

```
1df <- data.frame(rollnum = seq(1:10), h1 = 15:24, h2 = 81:90)
2df
```

{r}

Output:

```
1| rollnum | h1 | h2 |
2|--------- |---- |---- |
3| 1 | 15 | 81 |
4| 2 | 16 | 82 |
5| 3 | 17 | 83 |
6| 4 | 18 | 84 |
7| 5 | 19 | 85 |
8| 6 | 20 | 86 |
9| 7 | 21 | 87 |
10| 8 | 22 | 88 |
11| 9 | 23 | 89 |
12| 10 | 24 | 90 |
```

In this guide, you have learned about different types and structures of data, including concepts and techniques for creating and working with data types in R. To learn more about data science using R, please refer to the following guides: