Author avatar

Deepika Singh

Working with Data Types in R

Deepika Singh

  • Nov 6, 2019
  • 7 Min read
  • 6 Views
  • Nov 6, 2019
  • 7 Min read
  • 6 Views
Data
R

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.

Data Types

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

  1. 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().

1
2
t = "data types"
class(t)
{r}

Output:

1
[1] "character"
  1. Numeric: Decimal values like 2.3 is called numeric in R. It is the default computational data type.
1
2
3
N = 2.3
class(N)
 
{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:

1
is.integer(N)
{r}

Output:

1
[1] FALSE
  1. 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.
1
2
3
4
i = as.integer(3)
is.integer(i)

is.numeric(i)
{r}

Output:

1
2
3
[1] TRUE

[1] TRUE
  1. Logical: Logical values are often created by comparing two or more variables. These are denoted by Boolean values, TRUE or FALSE.
1
2
3
x = 100
y = 56
x < y
{r}

Output:

1
[1] FALSE 
  1. Complex: The complex variable is defined by the imaginary value i.

1
2
z = 3 + 2i 
class(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.

Data Structures

Vectors

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'.

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

class(n)
class(l)
class(s)
{r}

Output:

1
2
3
4
5
[1] "numeric"

[1] "list"

[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.

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

length(comb)
class(comb
{r}

Output:

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


[1] 11


[1] "character"

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

1
2
3
4
5
6
7
8
x = c(5, 3, 4) 
y = c(1, 2, 3)

#Arithmetic Operations
5 * x
x-y
x+y
x/y
{r}

Output:

1
2
3
4
5
6
7
[1] 25 15 20

[1] 4 1 1

[1] 6 5 7

[1] 5.000000 1.500000 1.333333 

Matrices

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.

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

Output:

1
2
3
     [,1] [,2] [,3]
[1,]   20   45   33
[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.

1
m[2, 2]
{r}

Output:

1
[1] 52

Lists

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.

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

l = list(name, place, age)   
print(l)
{r}

Output:

1
2
3
4
5
6
7
8
[[1]]
[1] "abhi" "ansh" "ajay"

[[2]]
[1] "delhi"  "mumbai" "pune"  

[[3]]
[1]  TRUE FALSE  TRUE FALSE FALSE

It is also possible to slice a list using the single square bracket " " operator.

1
2
3
l[2] 

l[c(2, 3)]
{r}

Output:

1
2
3
4
5
[[1]]
[1] "delhi"  "mumbai" "pune"  

[[2]]
[1]  TRUE FALSE  TRUE FALSE FALSE

Data Frame

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.

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

Output:

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

Conclusion

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:

0