Read a tab-separated file into R

Task

Create an R object that contains the data from a tab-separated file (which probably has the file extension “txt”).  We assume the data are rectangular — that is, that we can think of it as being in rows and columns.

Preparation

None, other than starting R.

Doing it

xassetCountrySector <- read.table(
  "https://www.portfolioprobe.com/R/blog/xassetCountrySector.txt",
  sep="\t", header=TRUE)

This command should work for you if you copy and paste it into an R session where you have access to the internet.

Explanation

Our call to the read.table function has used three arguments:

  • the location of the file — typically this would be a file on your file system (though, as here, it can also be a web address).
  • the sep argument says what character is used to separate items.
  • the header argument says whether or not the first line in the file contains labels.

The result is a data frame.  This is a rectangular object that can have different types of items in each column.

We have used the <- operator to make an assignment to the name “xassetCountrySector”. We could have used the = operator also — there is no difference in this case (and most cases). You won’t go wrong with object names if they start with a letter and include only letters, digits, dots (.) and underscores (_). Names are case-sensitive: “xassetCountrySector” is different than “xassetcountrysector”.

We can check to see if the object looks like what we expect:

> dim(xassetCountrySector)
[1] 350   2
> tail(xassetCountrySector)
        Country   Sector
XA980   Rodinia  Stringy
XA982    Amasia   Greedy
XA984 Laurentia   Greedy
XA986   Rodinia   Bouncy
XA990    Amasia Bustling
XA993   Baltica   Greedy

So the object we get has 350 rows and 2 columns. The last few rows are shown with tail. The head function shows the first few rows.

Further details

Alternative formulations

coerce to matrix

If all of the data in the file are numeric (except possibly row and column labels), then you may want to coerce the result into a matrix.  You would do that with something like:

> myMatrix <- as.matrix(read.table(filename, sep="\t",
+    header=TRUE)

no column names

If there are no column labels, then you would use header=FALSE or say nothing since FALSE is the default value.

row names

In this example the row names were automatically selected.  For this case it was a good choice, but you can control which data, if any, is selected to be the row names.

unknown file name

If you are on Windows and you aren’t exactly sure of the file name you want (or it is too much bother to type it), then you can use the file.choose function:

myObj <- read.table(file.choose(), sep="\t", header=TRUE)

This pops up a window in which you can choose the file that you want.

The tab character

The way to tell R that you mean the tab character is to say "\t".  Although this is written as two characters, it is really just one.  The backslash is the escape character in R.  It says to treat what follows it as special.

If you are on Windows, you may unwittingly get to experience the use of backslashes since Windows uses the backslash as the separator in file paths.  (But R automatically does the conversion in almost all cases.)

help

You can get the help for read.table with the command:

?read.table

Troubleshooting

file unknown

If you get an error and there is a message like:

cannot open file 'xxx.txt': No such file or directory

Then there are three likely possibilities of what has gone wrong:

  • You and R are not agreeing on what directory to look in
  • You have the wrong file name
  • The file really isn’t there

You can issue the R command:

getwd()

to see what R thinks is the working directory (the directory to look in for files or the directory to start relative paths from).  setwd changes the working directory.

You can list the files in the working directory with the command:

list.files()

lost in translation

When you are reading data from one place to another, there is always the chance of miscommunication.  There are many ways that such misunderstandings can arise.  The start of Circle 8.3 of The R Inferno gives a few of those ways as well as hints on how to figure out what is going wrong.

See also

Navigate