Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
R
Introduction to R
Introduction
Developed specifically for statistical analysis, R is a computer
language that implements many of the analytical tools
statisticians have developed for decision-making.
Click the link to download R. This puts the win.exe file in your
Windows computer, or the .pkg file in your Mac. In either
case, follow the usual installation procedures. When
installation is complete, Windows users see an R icon on
their desktop, Mac users see it in their Application folder.
Downloading RStudio
Here’s the URL:
www.rstudio.com/products/rstudio/download
Click the link for the installer for your computer, and again follow
the usual installation procedures.
You can run the R code in this console Environment tab keeps track of things you create (objects)
• Files tab
shows files
you create
• Plots tab
holds graphs
you create
from your
data.
• The
Packages
tab shows
add-ons
(called
packages)
you
downloaded
as part of the
The Help tab, provides links to a wealth of information about R and R installation.
Rstudio.
RStudio
click the larger of the two icons in the upper right corner of the Console
pane. That changes the appearance of RStudio so that it looks like:
File ⇒ Save As …
Type x into the Scripts pane and press Ctrl+R, and here’s what you see
in the Console pane:
>x
[1] 3 4 5
The 1 in square brackets is the label for the first value in the line of output. Here
you have only one value, of course.
What happens when R outputs many values over many lines? Each line gets a
bracketed numeric label, and the number corresponds to the first value in the
line. For example, if the output consists of 21 values and the 18th value is the
first one on the second line, the second line begins with [18].
Vectors
Now you can work with x.
Add all numbers in the vector. Typing
sum(x)
Take the average of the numbers in the vector x
mean(x)
Ctrl+R: executes to
> mean(x)
[1] 4
variance is a measure of how much a set of numbers
differs from their mean.
> var(x)
[1] 1
Vectors
Adding vectors: (element-wise sum)
a < - c(2,5,7)
b < - c(3,6,8)
c<-a+b
c
[1] 5 11 15
Comparing vectors:
a>b
[1] FALSE FALSE FALSE
Creating a vector of text names, not numerical content. (Note that each text value is enclosed
by quotation marks.)
>names<-c(“one”, “two”, “three”)
> names
[1] "one" "two" "three"
displaying the value of element position 3 in demo11. Note use of square bra
ckets here.
> demo11[3]
[1] 45
For example, here is some data (from a much larger data set) on the
luggage capacity, in cubic feet, of nine vehicles:
capacity <- c(14,13,14,13,16,NA,NA,20,NA)
Three of the vehicles are vans, and the term luggage capacity doesn’t
apply to them — hence, the three instances of NA. Here’s what
happens when you try to find the average of this group:
> mean(capacity)
[1] NA
To find the mean, you have to remove the NAs before you calculate:
> plot(demo11)
Creating a histogram of dmo8 values. NOTE that this histogram will OVERWRITE the prior
Plot. Thus, you should use the PC Snipping tool to copy/save the Plot image before creating
another graphic image.
> hist(demo8)
creating a boxplot of demo11 values The xlab phrase displays a label for the X axis.
>hist(demo11, col=rainbow(12))
> graphdemo<-c(2,4,6,8,9,3,4,7,11,14,18,20,9,6,4,4,11,14,20)
> graphdemo
[1] 2 4 6 8 9 3 4 7 11 14 18 20 9 6 4 4 11 14 20
> plot(graphdemo)
> boxplot(graphdemo)
> hist(graphdemo, xlab="my Monet”, col = rainbow(10))
BoxPlot
If there are any outliers and you don’t want to show the outliers, you
can set the outline = FALSE :
test <- c(142,23,41,10,7,93,17,174,420,13)
boxplot(test, outline =FALSE)
If you want to extend the range of the whiskers and suppress the
outliers inside this range, set range= 0
boxplot(test, range = 0)