Florian Oswald
2018-09-14
R!R.R and RStudioR: a statistical programming languageRStudio: an integrated development environment (IDE) to work with RR understands.4 + 1
[1] 5
8 / 2
[1] 4
log(exp(1))
[1] 1
# by the way: this is a comment! (R disregards it)
^:x = 2
x^3
[1] 8
sqrt(2)
[1] 1.414214
R built-in help:?log
?sin
?paste
?lm
help(lm) # help() is equivalent
??plot # get all help on keyword "plot"
help(ggplot,package="ggplot2") # show help from a certain package
R users contribute add-on data and functions as packagesinstall.packages("ggplot2")
library(ggplot2)
R package for you.if (!require("devtools")) install.packages("devtools")
library(devtools)
install_github(repo = "ScPoEcon/ScPoEconometrics")
library(ScPoEconometrics)
packageVersion("ScPoEconometrics")
[1] '0.1.3'
1.0, 2.11L, 2L, 42LTRUE and FALSE"a", "Statistics", "1 plus 2."Categorical or factor
c function creates vectors.c(1, 3, 5, 7, 8, 9)
[1] 1 3 5 7 8 9
c(42, "Statistics", TRUE)
[1] "42" "Statistics" "TRUE"
(y = 1:6)
[1] 1 2 3 4 5 6
seq creates a sequence from to in steps of by:seq(from = 1.5, to = 2.1, by = 0.1)
[1] 1.5 1.6 1.7 1.8 1.9 2.0 2.1
rep repeats items:rep("A", times = 10)
[1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
c(x, rep(seq(1, 9, 2), 3), c(1, 2, 3), 42, 2:4)
[1] 2 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 2 3 42 2 3 4
[1] 1 1 1 1 1
a:b is just short for construct a sequence from a to b. Create a vector the counts down from 10 to 0, i.e. it looks like [1] 10 9 8 7 6 5 4 3 2 1 0
rep to create a vector that looks like this: [1] 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3
help(), google or whatever) how to get the length of a vector in R!R uses 1 based indexing.[] to get the value at an indexx = c(1, 3, 5, 7, 8, 9)
x[2]
[1] 3
x[c(2,5)]
[1] 3 8
x[-4]
[1] 1 3 5 8 9
TRUE and FALSE to index:x = c(1, 3, 5, 7, 8, 9)
x[c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE)]
[1] 1 3 5 8
x > 3x > 3
[1] FALSE FALSE TRUE TRUE TRUE TRUE
x where x>3 is TRUE:x[ x > 3 ]
[1] 5 7 8 9
From the runif function get 10 numbers drawn from the uniform distribution and store in x.
get all the elements of x larger than 0.3, and store them in y.
using the function which, store the indices of all of those elements in iy.
[1] 1 2 3 4 9 10
y and x[iy] are identical.[1] TRUE TRUE TRUE TRUE TRUE TRUE
Matrix is a two-dimensional ArrayX = matrix(1:9, nrow = 3, ncol = 3)
X
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
X[1,2]
[1] 4
X[3, ]
[1] 3 6 9
X = matrix(1:4, 2, 2)
Y = matrix(4:1, 2, 2)
X * Y # equally for +, - and /
[,1] [,2]
[1,] 4 6
[2,] 6 4
X * Y is not matrix multiplication. All of above are element by element operations.%*%. What is X %*% Y for you?Create a vector containing 1,2,3,4,5 called v.
Create the (2,5) matrix m:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
m with v. Use the command %*%. What dimension does the output have?[1] 2 1
v %*% m not work?lists are more flexible:# works with and without fieldnames
ex_list = list(
a = c(1, 2, 3, 4),
b = TRUE,
c = "Hello!",
d = diag(2)
)
ex_list
$a
[1] 1 2 3 4
$b
[1] TRUE
$c
[1] "Hello!"
$d
[,1] [,2]
[1,] 1 0
[2,] 0 1
[] gets a sublist[[]] gets a list elementex_list[1]
$a
[1] 1 2 3 4
ex_list[[1]]
[1] 1 2 3 4
ex_list$d
[,1] [,2]
[1,] 1 0
[2,] 0 1
ex_list into your R session. Remember that list can hold any kind of R object. Like…another list! So, create a new list new_list that has two fields: a first field called “this” with string content "is awesome", and a second field called “ex_list” that contains ex_list.c from ex_list in new_list!new_list, the element under label this. Use the function paste to print the string R is awesome to your screen.names(new_list)
[1] "this" "ex_list"
paste("R",new_list$this)
[1] "R is awesome"
DataFrames are like spreadsheets.example_data = data.frame(x = c(1, 3, 5, 7),
y = c(rep("Hello", 3), "Goodbye"),
z = sample(c(TRUE,FALSE),size=4,replace=TRUE))
example_data
x y z
1 1 Hello FALSE
2 3 Hello FALSE
3 5 Hello TRUE
4 7 Goodbye FALSE
nrow(example_data)
[1] 4
ncol(example_data)
[1] 3
names(example_data)
[1] "x" "y" "z"
mtcars dataset is built-in to R.head(mtcars,n=3) # show first 3 rows
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
$ operator as in mtcars$mpgmtcars[,"mpg"] or mtcars[,1][,]# mpg[row condition, col index]
mtcars[mtcars$mpg > 32, c("cyl", "disp", "wt")]
cyl disp wt
Fiat 128 4 78.7 2.200
Toyota Corolla 4 71.1 1.835
subset(mtcars, subset = mpg > 32, select = c("cyl", "disp", "wt")]
mtcars?mpg?mpg for cars with more than 4 cylinders, i.e. with cyl>4?x = 1
y = "roses"
z = function(x){sqrt(x)}
condition.if (condition = TRUE) {
some R code
} else {
some other R code
}
x = 1
y = 3
if (x > y) { # test if x > y
# if TRUE
z = x * y # assign value to z
print("x is larger than y")
} else {
# if FALSE
z = x + 5 * y # assign other value to z
print("x is less than or equal to y")
}
z
for (i in 1:5){ # does not have to be 1:5!
print(i) # loop body: gets executed each time
# the value of i changes with each iteration
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
for (i in c("mangos","bananas","apples")){
print(paste("I love",i)) # the paste function pastes together strings
}
[1] "I love mangos"
[1] "I love bananas"
[1] "I love apples"
We often also see nested loops, which are just what its name suggests:
for (i in 2:3){
# first nest: for each i
for (j in c("mangos","bananas","apples")){
# second nest: for each j
print(paste("Can I get",i,j,"please?"))
}
}
[1] "Can I get 2 mangos please?"
[1] "Can I get 2 bananas please?"
[1] "Can I get 2 apples please?"
[1] "Can I get 3 mangos please?"
[1] "Can I get 3 bananas please?"
[1] "Can I get 3 apples please?"
say_hello tells R what to do when it you tell it say_hello().say_hello <- function(your_name = "Lord Vader"){
paste("You R most welcome,",your_name)
}
# we call the function by typing it's name with round brackets
say_hello()
[1] "You R most welcome, Lord Vader"
i is the iteratorSys.sleep(1) below the line that prints “i iterations to go”.ticking_bomb. it takes no arguments, it's body is the loop you wrote in the preceding question. The only think you should add to the body is a line after the loop finishes, printing “BOOOOM!” with print("BOOOOM!"). You can repeatedly redefine the function in the console, and try it out with ticking_bomb().