Week 2 from Quizlet Flashcards
control structures in R allow you to…
control the flow of execution of the program
if, else
testing a condition
for
execute a loop a fixed number of times
while
executes a loop while a condition is true
repeat
execute an infinite loop
break
break the execution of a loop
next
skip an interaction of a loop
return
exit a function
What is a loop index shown as
i
loop index
A counter variable used in For / Next loop.
seq_len()
Generate regular sequences. and the length
seq_along()
Generate regular sequences.
subset()
Return subsets of vectors, matrices or data frames which meet conditions.
How to stop an infinite loop
break
how to skip a part of a loop
next
How to tell R to exit a function and return a given value
return
what function do you use to construct a function?
function()
what do you use to get a standard deviation?
sd()
what does na.rm do?
removes NA’s from the object working on
how to remove NA’s and calculate the sd of a mydata file
sd(na.rm=TRUE, mydata)
What arguments are commonly specified when you call lm()
file, formula, subset,model.
lm(y-x, mydata, 1:100, model=FALSE)
what does NULL mean?
There’s nothing there
what is the “…” argument?
it indicates a variable number of arguments that are usually passed on to other functions. Often used when extending a function or if arguments can’t be known in advance.
Where should you write functions?
in a text file, by choosing “r script” in R
How wide should your code be maximum
80 columns of text
How to Return subsets of vectors, matrices or data frames which meet conditions.
subset()
What is an environment?
a collection of (symbol, value) pairs,
i.e x is a symbol and 3.14 might be its value
a function + an enviroment =
a closure or function closure
what is the global environment
the workspace
what is an empty environment
an environment without a parent
What does Lexical Scoping mean?
the values of free variables are searched for in the environment in which the function was defined
free variable
variables used in a function that are not local variables nor parameters of that function.
How to look at the arguments of a function
ls(environment(function name))
How to get the value of an argument in a function, ie n
get(“n”, environment(function name))
what class represents the date
Date class
what classes represent times
POSIXct class and POSIXlt class
how to convert date into a string
as.Date(“yr mnth day”)
How to get current time
Sys.time()
what function do you use to format how date/time are printed?
strptime()
What is an environment in R?
a collection of symbol/value pairs
The R language uses what type of scoping rule for resolving free variables?
lexical scoping
How are free variables in R functions resolved?
The values of free variables are searched for in the environment in which the function was defined
What is one of the consequences of the scoping rules used in R?
All objects must be stored in memory
In R, what is the parent frame?
It is the environment in which a function was called