V2 Flashcards

More Introduction to R

1
Q

name two options of control structures

A
  • branching : if, else if, switch

- looping: while, for, repeat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to code if-statement

A
if(class(box) == "character"){
right(box)
}else{
left(box)
}

example:
if(all(x<5)) print - check if all numbers are smaller than 5 in vector
if(any(x<5)) print - check if at leat 1 is smaller than 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to code switch-statement

A
switch(class(box)),
logical = left(x),
numeric = middle(x),
character= right (x)
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to code else if-statement

A

code same as if statement as second or third line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to code for-statement

A

for(x in 1:10){ box = box -x} - take x elements out of the box

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to code while-statement

A

box

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Comparisons operators (6)

A
== - equal to
!= - not equal to
< - smaller than
> - lager than 
>= - smaller or equal to
>= - larger or equal to
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to use &

A

& is vectorized, can be used for logical vectors

&& not vectorized, takes the first element from a vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what happens at combination of if and & , |

A
  • if needs single logical value

- so output from & and | cannot be used directly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to use pairwise statements

A

A&B - pairwise AND
A|B pairwise OR

  • > can store in a subset
  • > can also be used for matrices
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

use lapply (code)

A

lapply(X, FUN,…) - repeat a function to each element in a vector or list

na.rm to use if there is NA

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

use apply (code)

A

apply(X,Margin, FUN, …) repeat function to each row or column in a matrix
margin = 1 , rows
margin = 2 , columns
margin = c(1,2), rows and columns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what happens with lapply / apply results

A
  • normally give back list

- use unlist function to get vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to code a function

A

define a function with the keyword function

example:

boxwood

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

function parameter theory: pass-by-value

A

function parameters get copied into the function, changing them does not alter the state of the variable that was passed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

function parameter theory: pass-by-reference

A

function parameters are references to the variable passed into the function, changing them alters the state of the variable that was passed

17
Q

function parameter theory: pass-by-promised

A

function parameters are references to the variables passed into the function, when changing them, a copy is made. this way the state of the variable that was passed is not changed

18
Q

functions: parameters

A
  • in R function parameters get copied when changed inside a function (pass-by-promise)
  • so updating them is not visible from he outside
19
Q

functions: default parameters

A
  • we can set reasonable default for some variables
20
Q

whats a variadic function

A
  • a variadic function is a function which accepts a variable number of arguments
  • we can use … to specify variadic functions in R
21
Q

Functions: scope

A
  • intern is not visible from outside
  • this is called the scope of the variable
  • we can however access variables in our parent scope (is called bad practice but sometimes we have to. Reasons: being lazy, save RAM, plot functions use it to read the environment settings)
22
Q

Overview brackets

A

() : used when you call a function, used in control structure statements
[]: specify an index in a vector, matrix or data.frame
[[]]: specify an index in a list
{}: defines blocks of code, is used to surround expressions - what expressions belong to an if statement
- what expression belong to this function

23
Q

How to work with strings

A
  • strings are enclosed using: “ or ‘
  • combine strings using: paste
  • print them to screen using: print
  • print them to anywhere (screen and file) using: cat
24
Q

how to escape characters

A
Quotes: \" and \'
New line: \n
Tab: \t
Backslash: \\
backspace: \b
25
Q

how to use the seperate elements (code)

A

cat (“Hello”, “World\n”, sep=”,”) - seperate with comma

Hello, World

26
Q

runif()

A
  • get a random number
  • uniform distribution
  • every value has the same chance of being drawn
27
Q

rnorm()

A
  • get a random number in the gaussian distribution

- value near the mean have a higher chance of being drawn

28
Q

rpois()

A
  • get a random number in the poisson distribution

- numbers at the low en d of the distribution have a higher chance of occurring

29
Q

how to get a repeatable randomness

A
  • use set.seed() function

- same set.seed number produces same randomness