Today you will get introduced to the R/RStudio environment, specifically working in the Console window. The Console window will display executed code output and can also be used for quick code execution. However, any work done in the Console window will be lost once you exit RStudio. As such, copy and paste the answer from the console in the script after each question.
The console can be used as a calculator.
+-/*%%%/%^Evaluate the following expressions in the Console window
3 + 4 * 0 - (100 / 3)
Answer: -30.33333
(4 + 6) * (2 ^ 6)
Answer: 640
1 / 0
Answer: INF
10 ^ 10 ^ 10 ^ 10
Answer: INF
0 / 0
Answer: NaN
0.0000003 * 2
Answer: 6e-07
When you launch R/RStudio numerous functions are immediately available to you. These include many of the mathematical and statistical operations you know.
| R function | Purpose |
|---|---|
abs |
absolute value |
sin |
sine |
cos |
cosine |
tan |
tangent |
log |
logarithm |
exp |
exponential |
mean |
arithmetic mean |
median |
median |
sd |
standard deviation |
Evaluate the following expressions in the Console window.
absolute value of 7
Answer: 7
sine of 3.1415
Answer: 9.265359e-05
exponential 1
Answer: 2.718282
logarithm of 1, 6, 10
Answer:0,1.791759,2.302585
What logarithm did you just take? Was it the natural log, base 10,
base 2? natural log Type ?log in the
console. Another way to access help is to use
?<functionname>. A question mark that precedes a
function’s name or built in data object will open the help. (Note: put
?log in the console window).
Type the following in the Console window.
?sd - standard deviation?mtcars -?longleyWhat are mtcars and longley? mtcars = Motor
Trend Car Road Tests longley = Longley’s Economic Regression Data
The most important aspects of R’s help resource will be the description and examples given. Examples are always at the end of the help reference.
How many examples are given in the help of sd?
Answer: 1
Run the example provided in the help for sd in the
console.
Answer:0.5
Investigate what the following functions do:
sqrt
Answer: square root
round
Answer: rounds the number up or down
floor
Answer:Round Down
ceiling
Answer: Round up
You just found out about the longley dataset. Consider
Longley’s Economic Regression Data. This data set is built-in to R. That
means it is available immediately once RStudio is launched. Type
longley in your console to see the entire data set. The
same data is given below.
Answer the following questions about the longley
data set.
How many rows and columns does longley have? Hint:
check the nrow and ncol functions.
Answer: 16 rows and 7 rows
What is the difference between the first column of years and the
column with the label Year?
Answer: The column with the label has a int symbol
underneath it
Type head(longley) in the Console window. What does
this do? How about tail(longley)?
Answer: Head produces the first portion of the data and
Tail produces the last bit of the data
The data set longley is stored in R as a data frame.
Each column is a vector of the same variable type. We will learn about
these details later. For now, to access a specific vector use
longley$variable_name, where variable_name is
one of the variables in longley. For example,
longley$GNP
[1] 234.289 259.426 258.054 284.599 328.975 346.999 365.385 363.112 397.469
[10] 419.180 442.769 444.546 482.704 502.601 518.173 554.894
longley$Year
[1] 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
[16] 1962
give the GNP and Year vectors of data.
In your Console window get the following vectors: Note: You need not include the output for the vectors in the script. Just write down the R code used for 4-6 below.
Unemployed
Answer: longley$Unemployed
Population
Answer: longley$Population
Employed
Answer: longley$Employed
In your Console window compute the mean, median, and standard deviation for each of the vectors in 4 - 6. Type in your answers below. Tip: You can press the up-arrow on your keyboard to cycle through previous inputs in the Console and use that for calculation.
Unemployed
Answer:Mean = 319.3313 median = 314.35 sd =
93.44642
Population
Answer: Mean = 117.424 Median = 116.8035 sd =
6.956102
Employed
Answer: Mean = 65.317 Median = 65.504 sd =
3.511968
Compute the maximum and minimum for each of the vectors in 4 - 6. Hint: check the min and max functions.
summary function in R will give you many of these
statistics. For example,summary(longley$GNP)
Min. 1st Qu. Median Mean 3rd Qu. Max.
234.3 317.9 381.4 387.7 454.1 554.9
gives us the minimum, maximum, mean, and quartiles of the GNP vector of data.
Use the summary function on two variables of
your choice in longley. Do you think it makes sense to use
the summary function on the variable Year in
longley?
Answer: No it doesn’t make sense because those are
years that increase one at a time so not any actual data
Suppose it is 1962. Two economists are discussing employment. Each makes the following claim.
Economist A: The number of people employed has never been higher in the past 15 years. We have seen a gradual increase from 1947 to 1962.
Economist B: Employment has been range bound since 1947 and is at its lowest level since 1947.
Which economist is correct? Hint: Look at the
variable Employed across time. To make a simple plot use the function
plot. Create also a new variable, Employment, defined as
Employed/Population and look at it across time.
plot(x = longley$Year, y = longley$Employed)
longley$Employment <- longley$Employed/longley$Population
plot(x = longley$Year, y = longley$Employment)
Answer: Economist A
The graphics don’t look as attractive. The plot function
is part of the base R graphics and is used to make simple graphs. Later
we will learn about more advanced graphics using the
ggplot2 package. We will use the package extensively in the
future. However, we can tidy the above plot with the following code.
plot(x = longley$Year, y = longley$Employed, xlab = "Year", ylab = "Employed")
Tidy up the second plot by changing the labels
Answer:
The M1 ICA1 is an in-class group assignment. However, each member of the group who worked together in class will submit their in-class assignment at the end of the class time. Submit your work by uploading both your RMD and HTML files through D2L. Late work will not be accepted except under certain extraordinary circumstances.
To grade the ICAs, the instructional team will pick one submission at random from each group. Thus it is important that there is good intra- group communication and teamwork. Each group should ensure that everyone in the team understood, and completed the assignment.