13.1 Downloading Files
First we’ll deal with the situation in which the data we want is in a file somewhere on the Internet. In this case we need only download the file to our computer.
It’s a good idea to keep one’s work organized. Since we plan to begin downloading data files, we might as well create a directory dedicated to them. It’s easy to use the R Studio IDE to create the directory, but we might as well learn how the same task could be accomplished with an R-command. We will use the function dir.create()
.
dir.create("downloads")
We are ready to begin downloading. Let’s begin with a file32 at the URL:
The relevant function is download.file()
:
download.file(url = "https://github.com/homerhanumat/r-notes/blob/master/downloads/words.zip?raw=true", destfile = "downloads/words.zip")
The file should now be in the downloads
folder of your Home directory.
We see that it’s a zip-file. R can unzip it for us:
unzip(zipfile = "downloads/words.zip")
We now have the text-file words.txt
in our downloads
directory. In R Studio you can examine the contents of the file if you click on the file-name in your Files tab. You see something like this:
aa
aah
aahed
aahing
aahs
aal
...
The file is a fairly comprehensive list of words in the English language—all in lowercase, one word per line.
This file is a compressed version of
words.txt
file used in Think Python (Downey 2015).↩︎