2 | R: Programming Flashcards
(POLL)
2.1: Which of the following symbols are valid variable names
a) x
b) .x
c) X1
d) 1X
e) hello world
f) helloworld
g) i
h) in
i) f
j) for
a) x
b) .x
c) X1
f) helloworld
g) i
i) f
(POLL)
2.2: Which of the following operators are logical operators so return either TRUE or FALSE?
a) =
b) ==
c) <=
d) <-
e) %in%
f) %*%
g) +
h) -
b) ==
c) <=
e) %in%
(POLL)
2.3: Which of the following operators are assignment operators?
a) =
b) ==
c) ->
d) =>
e) <-
f) <=
g) «-
a) =
c) ->
e) <-
g) «- (superassignment)
(POLL)
2.4: Which of the following values are in R of type boolean?
a) true
b) True
c) TRUE
d) T
e) 1
f) false
g) False
h) FALSE
i) F j) 0
c) TRUE
d) T
e) 1
h) FALSE
i) F (interpreted as FALSE)
j) 0 (interpreted as FALSE)
(POLL)
2.5: Which of the following terms are valid R conditionals?
a) if
b) fi
c) elsif
d) elif
e) else if
f) else
g) when
a) if
e) else if
f) else
(POLL)
2.6: Which of the following terms are valid loop expressions in R?
a) break
b) continue
c) do
d) for
e) foreach
f) next
g) repeat
h) while
a) break
d) for
f) next
g) repeat
h) while
(POLL)
2.7) Which is the keyword to declare a function in R?
a) def
b) fun
c) func
d) function
e) none, we just use the assignment operator
d) function
(POLL)
2.8: What is the prefered order of arguments in the function declaration?
a) delegation,mandatory,optional
b) optional,mandatory,delegation
c) mandatory,optional,delegation
d) mandatory,delegation,optional
IMPORTANT
c) mandatory,optional,delegation
Operators are like _______ in R but ______ can be __________left and right.
Operators are like functions in R but arguments can be passed left and right.
R:
How can you declare your own operator? Give an example for the opposite of %in%
> '%ni%' <- Negate(‘%in%’) > LETTERS[1:8] [1] "A" "B" "C" "D" "E" "F" "G" "H" > LETTERS[1:8] %in% c('B','C','D') [1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE > LETTERS[1:8] %ni% c('B','C','D') [1] TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
R:
What two main types of control flow are there?
Conditionals
Loops
R:
Give an example for a loop using repeat and break, to print the numbers 3, 4 and 5.
Is there a better way to do this?
x=3 repeat { print (x); x=x+1; if (x > 5) { break } }
better:
~~~
x=3
for (i in x:5) {
print(i)
} # we like that most!!
~~~
R:
Is it a good idea to use loops?
Avoid loops in R if you can! (R < 4 ?)
* looping in R can be sometimes quite slow
* use vector operations if possible
* you can start with a loop but if ready: think about a vector operation
R:
what is rnorm?
The R function rnorm() is used for generating a vector of random numbers with a normal distribution.
R:
code for getting a matrix of 1000*100 numbers with a normal distribution.
> M = matrix(rnorm(100*1000), nrow=1000, ncol=100)
R:
Functions are ________ like variables. They can be called with or without __________.
objects, arguments
R:
Structure when creating a function?
``` _____________ = ________ (__________) {
___________________
____________# if required
}
~~~
- name of function
- function
- arguments
- implementation
- return(value)
``` NameOfFunction = function (Argument(s)) {
Implementation
return(value) # if required
}
~~~
R:
How to distinguish between mandatory and optional arguments when declaring a function?
- mandatory: without = sign
- optional: with = sign and default value
R:
Write a function for the coefficient of variation for a given numeric vector
myCV = function (x) { cv = 100 * sd(x, na.rm=TRUE) / mean(x, na.rm=TRUE) return(cv) }
R:
How can you look at code for a function in R?
two ways:
> body(functionName)
> functionName
(the function is an object in R!)
R:
Must you always enter arguments in the right order?
You can use different argument orders if you use arg.name=value syntax:
lm(data = mydata, y ~ x, model=FALSE, 1:20)
R:
What is ‘…’ in R?
Three dot / ellipsis argument
→ functions can:
- accept additional args w/o defining in signature
- be more flexible/adaptable to diff. situations.
R:
What does dispatching functions mean? Give an example
R Dispatching functions
old S3 approach (1988) - still most popular
> var = 3 > class(var) [1] "numeric" > class(var) = "test" ; > class(var) [1] "test" > print.test = function (x) { print (paste("this is the method of test:",x)) } > print(var) [1] "this is the method of test: 3"
R:
What is require() in R?
used to load packages inside functions