Episode 1
Introduction
This tutorial is the third part of my Ultimate Beginner R Tutorials and is the first episode of the R Data Structures Series, therefore it assumes that the reader is aware of the content in the first part and the second part.
To read the first part, you can follow this link. To read the second part, you can follow this link.
In this part, we will cover data structures in the R language and how to deal with them.
Data Structures
A data structure is a specialized format for organizing, processing, retrieving and storing data. In R language, we have 6 natural data structures, these include
- Vectors
- Lists
- Matrices
- Arrays
- Data Frames
- Factors
Vectors
- In R lang, a vector is a list of items that are of the same type.
- we use the c() function to create vectors in R.
Example
numbers <- c(1, 2, 3)
print(numbers)
To create vectors with numerical values in a sequence, we use a miscellaneous operator : .
Example
numbers <- 1:10
- Notice that in the sequence created above, values increase by 1.
- To make bigger or smaller steps in a sequence, we use the seq() function.
Example
numbers <- seq(from = 0, to = 100, by = 10)
print(numbers)
Note that the seq() function has three parameters, from, where the sequence starts, to, where the sequence stops, and by is the interval of the sequence.
Vector Length
Sometimes we want to find out how many items are in a vector. To do this, we use the length() function.
Example
sweet_fruits <- c("banana", "apple", "shafara")
length(fruits)
Sorting a vector
For some reason, you may want to arrange vector items either alphabetically or numerically, to do this, we use the sort() function.
Example
sweet_fruits <- c("banana", "apple", "shafara")
numbers <- c(15, 2, 45, 1, 8, 4)
# to sort strings/fruits
sort(sweet_fruits)
#to sort numbers
sort(numbers)
Accessing vectors
- You can access items in a vector using the square brackets [].
- In R, unlike most languages, indexing starts from 1, so the first item has an index of 1, the next 2, and so on.
Example
sweet_fruits <- c("banana", "apple", "shafara")
# return the 3rd fruit
sweet_fruits[3]
You can access multiple items elements by referring to different index positions with the c() function.
Example
sweet_fruits <- c("banana", "apple", "shafara")
# access the first and third fruit
fruits[c(1, 3)]
Change an item
Many times, for some reason you may want to change a specific item in the vector, to do this, we refer to the index of the item.
Example
sweet_fruits <- c("banana", "apple", "shafara")
# change the third fruit from "shafara" to "mango"
fruits[3] <- "mango"
Repeat vectors
We use the rep() to repeat vectors
Example
# repeat each of the values
repeat_each <- rep(c(1,2,3), each = 3)
# repeat vector sequence
repeat_times <- rep(c(1,2,3), times = 3)
print(repeat_each)
print(repeat_times)
The best way to learn to program is by doing, hopefully, you are doing what is discussed here so that you get results.
Summary
What we have learned:
- What is a Data Structure
- What is a Vector
- Getting vector length
- Sorting the vector
- Accessing vector items
- Changing an item
- Repeating vectors
Hello, I Am Kibekityo Juma Shafara, a passionate web developer and statistician, currently am enjoying writing articles about programming. If you like my articles, follow me and/or subscribe to my newsletter so that you do not miss out on my upcoming articles.