Reading delimited data

January 7, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

In the exercises below we cover the basics of reading delimited data. Before proceeding, first read section 7.1 of An Introduction to R, and the help pages for the read.table function.

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 read 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 read it from there.

Exercise 1
Read the file Table0.txt.
a) Change the names of the columns to Name, Age, Height, Weight and Sex.
b) Change the row names so that they are the same as Name, and remove the variable Name.

Exercise 2
Read the file Table1.txt, how many rows and columns does it have?
a) Reread the file and make the variable Name be the row names. Make sure you read the variable as characters and not as factors.

Exercise 3
Read the file Table2.txt, watch out for the first line.

Exercise 4
Read the file Table3.txt, watch out for the first line and the missing values.

Exercise 5
Read the file Table4.txt, watch out for the missing values and the decimal separator.

Exercise 6
Read the file Table5.txt, watch out for the missing values and the decimal separator and the separator.

Exercise 7
Read the file states1.csv, the names of the states should be the row names.

Exercise 8
Read the file states2.csv, the names of the states should be the row names, watch out for the decimal separator and the separator.

Exercise 9
Read the file states2.csv, watch out for the same as the last exercise plus the missing values. Add to the previous dataset, column-wise.

Exercise 10
Read the file Table6.txt, check out the file first. Notice that the information is repeated, we only want the first non-repeated ones. Make sure to create only characters not factors this time around. Lastly, we don’t want the comments.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)