Beginner Guide To R and R Studio V1
Beginner Guide To R and R Studio V1
Introduction
Once logged into the RStudio server you will see the following.
Try not to overwhelmed. Although R Studio is very powerful,
for our intro class we will use it mainly as a fancy graphing
calculator. In more advance classes, we explore the software
further.
To make a new script, we can go to File > New File > R Script or
we can click on the new icon and choose R Script as shown
below.
Now we have 4 windows: clockwise from left, the unsaved R
Script file, Environment and history panel, File and Plots panel
and the console.
To save we use the universal floppy disk icon for save. After
saving the script file we can see the newly saved file in the File
panel in the lower right corner.
Part One: Arithmetic and Variables
Type 8+3 and press return. It doesn’t matter whether there are
space between the values or not.
>8 + 3
[1] 11
# multiplying x and y
>x*y
[1] 33
Caution: If you use the same data structure name as one that
you have previously used, then R will overwrite the previous
information with the new information without warning the
user.
Example Two: Data Vector and Loading Packages,
and Basic Stats
Since it is a very small set of data, we can type it in using the “c”
command, short for combine or concatenate:
# calculate mean
>mean(ourdata)
[1] 2.0625
# calculate median
>median(ourdata)
[1] 1.75
# calculate range
>range(ourdata)
[1] -3 8
# calculate variance
>var(ourdata)
[1] 10.17411
This data set may be read into RStudio as follows (and this is
how we will read in all data sets for this class).
# median of MPGs
>median(mydata$mpg)
[1] 20
plot() function needs two set of data points and will plot them
against each other. Then, I will introduce some of the options
we can use to make out plots more detailed, informative, and
representative.
Now, let make a scatterplot of weight and price of the car data.
We put the essential price and weight in to the plot function
and rest is just to make the plot prettier. The pch argument
defines the shape of the dots.
# attaching mydata to the environment
>attach(mydata)
Making Histograms
Making Boxplots