library(learnr)
library(testwhat)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
tutorial_options(exercise.checker = testwhat_learnr)

R Addition

We will start off seeing how R can be used as a calculator.

The code below shows how to add 3 and 5 together. Above that line there is some additional text with # (hashtag/pound) before it. These are comments that will not effect the output of R. R ignores anything that comes after # on that line.

Instructions:

Add together 7 and 11

#### To add 3 and 5 type
3 + 5

### Now add 7 and 11
#### To add 3 and 5 type
3 + 5

### Now add 7 and 11 
7 + 11
ex() %>% check_output_expr("7+11")

More R calculator functions

R can do more than just addition but can also do subtraction, multiplication and division.

Instructions:`

Do the following problems:

### subtraction uses -

## multiplication uses *

## division uses /
### subtraction uses -
8 - 3

## multiplication uses *
6 * 2

## division uses / 
9 / 2
ex() %>% {
  check_output_expr(., "8-3")
  check_output_expr(., "6*2")
  check_output_expr(., "9/2")
}

Multiple Operations

You do not have to do everything in one line. You can also chain operations together just like you would in math.

Just like you should, R follows PEMDAS rules of operation.

Instructions:

Type in two different operations.

1) Multiply 3 by 4 and then subtract 1 from it (your answer should be 11). 2) Add 3 and 2 together and then multiple that by 5 (your answer should be 25).

### Remember you want to keep PEMDAS in mind.
### For example:
3 + 4 * 2
### is different from
(3 + 4) * 2
### Remember you want to keep PEMDAS in mind.
### For example:
3 + 4 * 2
### is different from
(3 + 4) * 2

3 * 4 - 1
(2 + 3) * 5
ex() %>% {
  check_output_expr(., "3*4-1")
  check_output_expr(., "(2+3)*5")
}

More advanced operations

And finally, R also can do exponentiation as well as all the other trig functions that you might remember (sine, cosine, tangent). R even has a lot of a standard variables like pi. We won't use most of these, but just so you see how it works lets try a few.

Instructions:

You are going to square 3, take the square root of 2 and then finally just double pi.

### To exponentiate you use the ^ symbol (shift+6 on your keyboard). 
### Also square roots are the same as exponentiating by (1/2) so the square root of 5 is..
5^(1/2) 

### Now square 3

### Take the square root of 2

### To use pi, all you have to write is pi
pi

### You can use this just like a regular number, so double pi below
### To exponentiate you use the ^ symbol (shift+6 on your keyboard). 
### Also square roots are the same as exponentiating by (1/2) so the square root of 5 is..
5^(1/2) 

### Now square 3
3^2
### Take the square root of 2
2^(1/2)
### To use pi, all you have to write is pi
pi

### You can use this just like a regular number, so double pi below
2*pi
ex() %>% {
    check_output_expr(., "3^2")
    check_output_expr(., "2^(1/2)")
    check_output_expr(., "2*pi")
}

Intro to variables

When we talk about a variable in R, we don't necessarily mean the exact same thing as a variable in general. In R when we say variable we just mean something that we've stored in R and can recall. In the below example you will see two types of things you can store and how to get them back.

Instructions:

Variables are created in R by assigning something to a word or letter. The word or letter is the variable. You use <- to assign something to it. So if you want to assign the number 4 to the variable kevin you would write: kevin <- 4. This assigns 4 to kevin. You can then recall kevin, by typing kevin and hitting enter (this is often refered to as calling a variable).

For this assignment I want you to create a variable named pol and assign it the value 25 and then call that variable to show what it is.

##### again we can start by assigning 4 to kevin

kevin <- 4

#### If you just run the above then there will be no output. 
#### If you want to see what the variable kevin is, then you can call it by just writing it out:
kevin 

#### now assign 25 to pol, and then call it again
##### again we can start by assigning 4 to kevin

kevin <- 4

#### If you just run the above then there will be no output. 
#### If you want to see what the variable kevin is, then you can call it by just writing it out:
kevin 

#### now assign 25 to pol, and then call it again
pol <- 25
pol
ex() %>% check_object("pol") %>% check_equal()

More than just numbers

Variables don't just have to store numbers, they can also store words or sentences (called strings) or a list of numbers (called vectors). See the code to the right to see how you create these two types of variables.

Instructions:

Create one vector called x with the value "Political Science" and another vector called y that is a list of the numbers 1 through 5.

### Creating a variable that is a word or sentence is just like doing it with a number
### but you need to suround the word(s) with quotation marks.

miami <- "Oxford Ohio"
miami

### Creating a vector (a list of numbers) is more complicated. 
### you need to surround them with paranthesis and put a c infront of the
### first paranthesis. Each number should be separated by a comma

primes <- c(1, 3, 5, 7, 11)

### now put your code for x and y below
### Creating a variable that is a word or sentence is just like doing it with a number
### but you need to suround the word(s) with quotation marks.

miami <- "Oxford Ohio"
miami

### Creating a vector (a list of numbers) is more complicated. 
### you need to surround them with paranthesis and put a c infront of the
### first paranthesis. Each number should be separated by a comma

primes <- c(1, 3, 5, 7, 11)

### now put your code for x and y below
x <- "Political Science"
y <- c(1, 2, 3, 4, 5)
ex() %>% {
  check_object(., "x") %>% check_equal()
  check_object(., "y") %>% check_equal()
}

So what?

Variables are useful in R because we can easily manipulate vectors without having to type them over and over again. You can do all the same sort of operations we did last week, but on variables.

For example, the current population of Ohio is 11,658,609 if you want to figure out what 10% or 25% of the population is you can store the population as a variable and then multiply it by .1 and .25.

Instructions:

The Pennsylvania population is 12,805,537. Store it as the variable pa.pop and then figure out what 10%, 25% and 50% of the population of Pennsylvania is by multiplying that variable.

## store the population of ohio:
ohio.pop <- 11658609

## multiply it by .25 to figure out what 25% of the population is:
.25 * ohio.pop

## multiple it by .10 to figure out what 10% of the population is:
.10 * ohio.pop
## store the population of ohio:
ohio.pop <- 11658609 ### note that you can't include commas in the numbers

## multiply it by .25 to figure out what 25% of the population is:
.25 * ohio.pop

## multiple it by .10 to figure out what 10% of the population is:
.10 * ohio.pop

pa.pop <- 12805537
pa.pop * .25
pa.pop * .10
pa.pop * .50
ex() %>% check_correct(
  {
    check_output_expr(., "pa.pop * .25")
    check_output_expr(.,"pa.pop * .5")
    check_output_expr(.,"pa.pop * .1")
   },
  check_object(., "pa.pop") %>% check_equal()
)


reuning/POL306 documentation built on Sept. 19, 2023, 10:47 a.m.