In the exercises below we cover the basics of the scan
function. Before proceeding, first read section 7.2 of An Introduction to R.
Answers to the exercises are available here.
For each exercise we provide a data set that can be accessed through the link shown in the exercise. You can scan the data from this link directly (clicking on it will show the url in the address bar of your browser), or you can download the data first to a local directory, and scan it from there.
Exercise 1
Read the file scan01.txt as a vector.
Exercise 2
Read the file scan02.txt:
a) As a vector
b) As a matrix with 10 rows.
Exercise 3
Read the file scan03.txt as a vector.
Exercise 4
Read the file scan04.txt as a matrix of 2 columns. Read only the 5 first rows. Finally, convert it to a data frame.
Exercise 5
Read the file scan05.txt as a list.
Exercise 6
Read the file scan05.txt as a list, read only the first 50 rows.
Exercise 7
Read the file scan.csv as a list.
Exercise 8
Read the file scan06.txt as a list. Read 10 rows of data.
Exercise 9
Read the file scan07.txt as a list.
Exercise 10
Read the file scan2.csv as a list.
Solution to Exercise 4 is wrong, unless there is no relation between numbers in first column and numbers in second column.
With the solution like here we have 171 and 28 on first row, but the table has 171 and 20.
The “correct” solution would be:
df <- data.frame(matrix(scan("http://r-exercises.com/wp-content/uploads/2015/12/scan04.txt", nlines=5), nrow = 5, byrow=TRUE))