Graphics Programs Reference
In-Depth Information
Set your working directory in R to the same directory as your data file via
the main menu if you want to load the data locally from your own com-
puter, You can also use the setwd() function.
If you're new to programming, this probably looks cryptic, so now break
it down so you can understand it. This is one line of R code. You load data
with the read.csv() command, and it has three arguments. The first is the
location of your data, which in this case is a URL.
The second argument, sep , specifies what character separates the col-
umns in the data file. It's a comma-delimited file, so specify the comma. If
it were a tab-delimited file, you would use \t instead of a comma to indi-
cate the separator was a tab.
The last argument, header , tells R that the data file has a header, which
contains the name of each column. The first column is year, the second
is the winner's name, the third is number of HDBs eaten, and the fourth
is the competitor's resident country. I've also added a new field that you
might have noticed: new record. If the world record was broken on a year,
the value is 1. Otherwise it is 0. You can put this to use soon.
The data is now loaded into R and is available via the hotdogs variable.
Technically, the data is stored as a data frame, which isn't totally impor-
tant but worth noting. Here's what the beginning of the data frame looks
like if you type hotdogs.
Year Winner Dogs.eaten Country New.record
1 1980 Paul Siederman & Joe Baldini 9.10 United States 0
2 1981 Thomas DeBerry 11.00 United States 0
3 1982 Steven Abrams 11.00 United States 0
4 1983 Luis Llamas 19.50 Mexico 1
5 1984 Birgit Felden 9.50 Germany 0
The spaces in the column names have been replaced with periods. Dogs
eaten is now Dogs.eaten . It's the same thing with New record . To access a
specific column of data, you use the name of the data frame followed by
a dollar sign ($) and then the column name. For example, if you want to
access Dogs.eaten you enter the following:
hotdogs$Dogs.eaten
Now that you have the data in R, you can go straight to graphing with the
barplot() command.
barplot(hotdogs$Dogs.eaten)
Search WWH ::




Custom Search