This is the R markdown (rmd) exercise file that accompanies the lecture notes. It will be your sandbox! Fill in the R code chunks, experiment, and have fun!

What is R?

Why R?

Why use R? Here are the 5 reasons:

https://www.r-bloggers.com/why-use-r-five-reasons/

It is nice that everyone has the opportunity to learn R.

What are the Most Disliked Programming Languages?

perl, delphi

Find it out from this article:

https://stackoverflow.blog/2017/10/31/disliked-programming-languages/

Python & R vs. SPSS & SAS

A comparison between the statistical programming languages SAS, SPSS, R and Python:

https://www.r-bloggers.com/python-r-vs-spss-sas/

Hadley Wickham has a good point about choosing between R and a point-and-click statistical software (such as SPSS or Stata), and the advantage of using R instead of Python for beginners in data analysis:

https://r-posts.com/advice-to-young-and-old-programmers-a-conversation-with-hadley-wickham/

R as a Calculator

R as a calculator

How to do arithmetics in R? Try a few of the following arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus (Remainder from division)
%/% Integer Division (Quotient from division)

Code Chunk is below

2 + 3
## [1] 5
2 ^ 5 
## [1] 32

Spreadsheet: Which of these operators are the same as in a spreadsheet? Google to find out how to perform modulus (finding remainder) in spreadsheets.

Three different ways to run R codes

Try the three different ways to run R codes:

  • In the console
  • In an R script
  • In an rmd (R Markdown) file

Why use R Markdown?

Google “markdown cheatsheet” for a quick reference to markdown syntax

Go to rpubs.com to see examples of reports written in R Markdown.

Go to (https://rmarkdown.rstudio.com/) for a quick tutorial on R Markdown.

Cosma Shalizi has a much more extensive manual at:

http://www.stat.cmu.edu/~cshalizi/rmarkdown/

Yes there is a book on R Markdown if you want to learn deep:

https://bookdown.org/yihui/rmarkdown/

Variables

Variables

Define variables for present value, interest rate, and compounding period, then use the variables to calulate the future value:

# create three variables 
pv <- 100 # This is present value 
r <- 0.1
n <- 2

Here we calculate the future value in two years?

fv <- pv * (1+r) ^ n # This calculates the future value
fv
## [1] 121

right arrow is valid as well ->

Anything after # is a comment. Use comments to make your code more readable!

Three equivalent ways to assign value to a variable

What are the different ways to assign value to a variable? Try them out:

q <- 50
a = 55

Which do you think is the best way?

Variable Names

The following will produce error

.2way <- 10

Are the following variable names valid?

  • w2
  • price_IBM
  • shares-of-Google
  • .Price
  • .2way
  • TRUE

Try to assign values to them to find out:

w2 and price_IBM were assigned values

w2 <- 20
price_IBM <- 25
shares-of-Google <- 30
.Price <- 35
.2way <- 40
TRUE <-  45

It helpful to name your variables MEANINGFULLY!

R is case sensitive

shares <- 100
shares
## [1] 100

The following code will produce error:

SHARES

ls()

ls()
## [1] "a"      "fv"     "n"      "pv"     "q"      "r"      "shares"
ls(pattern = "p")
## [1] "pv"

How to find out all existing variables in the current memory environment? A: We use the environment tab to see once we run all chunks

rm()

rm(pv)
ls()
## [1] "a"      "fv"     "n"      "q"      "r"      "shares"
rm(r, n)
rm(list = ls())
ls()
## character(0)

How to remove variables from the memory?

We use the rm function

b = 10
c = 5
list_of_variable <- ls()
list_of_variable
## [1] "b" "c"
rm(list = list_of_variable )
ls()
## [1] "list_of_variable"

Finding help

What are the different ways to find help?

Utilizing help(ls), Google and ?ls

help(ls)
?ls
.a <- 10
ls()
## [1] "list_of_variable"
.a
## [1] 10
ls(name = .GlobalEnv, all.names = TRUE)
## [1] ".a"               "list_of_variable"

Use help() or ? to load the documentation to find out about more about ls() and rm().

help(ls)

There is an R specific Internet search engine at http://www.rseek.org for more assistance.

Stack Overflow is a programming user community with an R thread at http://stackoverflow.com/questions/tagged/r .

Basic Data Types

Basic Data Types

Numberic Integer Complex Logical Character

http://www.r-tutor.com/r-introduction/basic-data-types

Numeric

Assign a decimal value to a variable, and check its class type using class():

x <- 100.5
x
## [1] 100.5
class(x)
## [1] "numeric"

In the following code, a variable k has been created. Is it numeric? Use class() to find out:

k <- 1

# find out about the class of k:

Integer

Experiment with the two ways to create integer variables:

y <- 3L
y
## [1] 3
class(y)
## [1] "integer"

What is the difference between numeric and integer? Do you need to worry about it? Read the following post at stackoverflow: https://stackoverflow.com/questions/23660094

Numeric classes gets converted using double precision and integers do not. We will not have to worry about it in this class

Character

What is your favorite stock? Assign it as a charcter string to a variable, and print it out:

symbol <- "APPL"
symbol
## [1] "APPL"
class(symbol)
## [1] "character"
name <- "Michael Lewis"

Spreadsheet: The string can be represented by starting with a single quote.

Date Values

Readings:

https://www.statmethods.net/input/dates.html

Create two dates, and find out he number of days between these two dates.

date1 <- as.Date("2019-01-01")
date2 <- as.Date("2019-08-23")
date1
## [1] "2019-01-01"
date2
## [1] "2019-08-23"
date2 - date1
## Time difference of 234 days

Google to find out how to return today’s date.

Sys.Date()
## [1] "2021-09-07"

Printing Dates

Experiment with the different formats to print your date variable:

format(date2, format="%m/%d/%Y")
## [1] "08/23/2019"

Character to Date

How would you convert a character string “Jun 23, 2008” into date?

date3 <- as.Date("2008-06-23")
format(date3, format="%b %d, %Y")
## [1] "Jun 23, 2008"

Logical

(my_answers <- TRUE)
## [1] TRUE
r <-  0.15
r > 0.2
## [1] FALSE

Can you assign the lower case true or false to create a logical variable? Exccute the x <- true to find out:

No you can not assign lower case.

x <- true

Spreadsheet: A spreadsheet also uses TRUE and FALSE for logical values.

Relational Operators: Examples

Build some expressions using the relational operators:

r == 0.15
## [1] TRUE
r != 0.2
## [1] TRUE

Logical Operators: Examples

Build some expressions using the relational operators:

Q: Is r between 10% and 20%?

r > 0.1 & r < 0.2
## [1] TRUE

Q: Is r outside the range between 10% and 20%?

r <- 0.05
r < 0.1 | r > 0.2 
## [1] TRUE

Q: Is r not greater than 0.2?

! r > 0.2
## [1] TRUE

Special Values

What does -1/0 return? What is the class of Inf and NaN?

They are undefined values

-1/0
## [1] -Inf
0/0
## [1] NaN