A vector is the simplest data structure used in many programming languages. It consists of a collection of elements, which can be identified by an index. The author often imagines arrays and vectors as a row of terrace houses in the United Kingdom. They all look the same from the outside, but inside they are uniquely decorated. Each house has a unique number too. A vector in computer science must not be confused with Euclidean vectors; arrays and vectors relates more to a set.
In R, one-dimensional arrays have a fixed numbers of elements and referred as vector. Every element must be of the same data type; it is homogeneous. The number of the first element is 1; other programming languages may use 0. Vectors can be added, subtracted, multiplied and divided.
In R, vector are created using the c notation. For example, a vector containing the first three primes are defined as
Create two vectors called prime and non.prime in a script or in the console.
Find the sum, product, quotient and differences between the prime and non.prime vectors.
Print the first and last element of each vector.
5. Sort the two vectors and print the ordered vectors.
Character vectors manipulation
Create the days vector, type days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
Create another vector, referred as months.
Try to find the sum, product, quotient and differences between days and months. Some errors should be shown.
Sort the two vectors and print the ordered vectors.
Logical vectors manipulation
Create two boolean vectors named sunny.days and rainy.days. Both of them should have 7 elements.
Type rainy.days <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE)
Type sunny.days <- c(FALSE,TRUE, FALSE, FALSE, TRUE, TRUE, FALSE)
Find the sum, product, quotient and differences between the rainy.days and sunny.days vectors. Some numerical values should be found. It is worth noting, TRUE is represented by 1 and FALSE by 0. Some of the results have produced some numerical vectors, not logical vectors.
Sort the two vectors and print the ordered vectors.
Using vectors incorrectly
Create the vector incorrect.vector.1 <-c(1,”2”,3). The vector has been converted as a character vector.
Create the vector incorrect.vector.2 <-c(1,TRUE,3, FALSE). The vector has been converted to a numerical vector.
List is also a heterogeneous one-dimensional data structure; it can consist of elements of the same data type or different data types. Each element has a unique index too; the index of the first element is 1 and the last one is the length of the list.
List are useful to group information of different data types. In R, list are created with the function list. The example below illustrates how a list can be created. It has contains a date, the humidity, the temperature and the wind speed with direction. Some characters, integer and numerical values are stored in the list as a complete set. A list can also holds some vectors.
A double bracket is used to extract the elements of a list; for example weather.condition[].
Create the weather.condition list using the same structure as above. Try to add your own values.
Display every element of the list.
Display the first two elements of the list; type weather.condition[1:2]
Convert the weather.condition into a vector. Type unlist(weather.condition). You will see all the values have been converted as character elements.
More complex list made of vectors
Let’s create a two-dimensional structure using some vectors. Some vectors of the same lengths can be used. Use again the vectors created in the previous practice. Type weather.list <- list(days, rainy.days, sunny.days).
Let’s build a character variable that shows the values for the Monday. Type paste(weather.list[], weather.list[],weather.list[])
Operations not possible on lists
Create a list of prime numbers; type prime.list <- list(2,3,5)
Create a list of non prime numbers; non.prime.list <- list(1,4,-1)
Create two vectors called prime and non.prime. It should not work.
A data frame is a data structure that stores data as a two-dimensional table, with some rows and columns. Similarly has a table in a database, each column can store data of the same data type. For that reason, each row may have an heterogenous collection of data. Each row must have the same length.
Elements of a data frame can be shown using a “[“ and “]” notation: [row or rows, column or columns].
A new packages referred Tibble has been created to simplify the data frame manipulation. It is worth reading about this new method of using two-dimensional tables.
Copy the downloaded file into your project folder.
Import the data. You have two options:
Import the data using one of these options:
Using the Files tool of R Studio (Botton right of the screen), find the downloaded file. Click on the file and select import Dataset…. Change the name to weather and click on Import.
Alternatively, on the Environment (top right corner), click on the Import dataset option. Select the second option (i.e., From Text (readr). Use the Browse utility to find and select weather.csv. Change the name to weather and click on Import.
A second data frame should have been created in the Environment. Open the new data frame in a new tab and explore the data.
Practice - Extracting some information from a data frame.
Let’s show the humidity3pm type weather[ ,13].
Let’s show the first 3 rows of the data frame: type weather[1:3,]
Let’s show all the temperatures; type weather[,3]
Let’s show the first two columns of the data frame; type weather[,1:2]
Type weather[1:3,1:2]. The first 3 rows and first two columns are shown.
Show the values of the last three rows.
Show the values of the second column.
Show the values of last three rows and the second column.