knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
\newline
\newline
?Rdlazer help(Rdlazer) help("Rdlazer")
(
), brackets ( [
) and braces ( {
)#
) is a comment sign#
in a line is considered a comment rather than code and will not executedL
to a digit: 1L
NA
for not availableNULL
, a "nothing" object"
) or single ('
)\
\t
for tab, \n
for new line==
test identity; is this the same as that?!
negate; reverse a logical value!=
test difference; is this different to that?&
AND; are both items TRUE; analogous to multiplication|
OR; is any of the two items TRUE; analogous to addition+
, -
, *
, /
, ^
add, subtract, multiply, divide, raise to power%%
, %/%
division remainder, modulus==
, !=
test equality/inequality>
, <
, >=
, <=
greater than, less than, greater or equal, less or equal-
).
) are hidden<-
, ->
and =
(name on left and value on right)<-
, with spaces around it for intelligibility<<-
and ->>
also assign variables but do not use them unless you have to (more on this later)foo(x, y)
name = value
pairs\newline
...
it is used in two situations:
some functions work as infix operators: the call is structured x op y
rather than foo(x, y)
%in%
or +
or <-
Use the foo
function to play around with function calls.
several special constructs allow for controlling code behavior
1. perform actions conditionally with if
and else
:
if (condition) {
expression
}
TRUE
\newline
if (condition) {
expression1
} else {
expression2
}
FALSE
\newline
if (condition1) { if (condition2) { expression1 } else { expression2 } else { expression3 } }
TRUE
TRUE
\newline
if (condition1) { exrpession1 } else if (condition2) { exrpession2 } else if (condition3) { exrpession3 } else { exrpession4 }
TRUE
, the others are not testedTRUE
will be executedelse
!\newline
2. perform actions iteratively with loops:
next
command will interrupt an iteration and move the loop on to the next onebreak
command will terminate the loop immediately\newline
for (iterator in sequence) {
expression1
expression2
expression...
}
\newline
the expressions in the body of the loop can make use of the iterator, e.g. pass it to functions
a repeat loop:
repeat { expression1 expression2 expression... }
\newline
while(condition) {
expression1
expression2
expression...
}
TRUE
The following material will be covered in detail in following lectures. This is just a sneak peak that will be necessary to solve the homework assignments.
1) vectors
- the basic data structure in R is called a vector
- it is a sequence of items of the same type; thus a vector also has a type
- the number of items in a vector constitutes its length
- vectors are most commonly created with the function c
# build a character vector c('one', 'two', 'three', 'four')
2) sets
- a vector can be thought of as a set of elements
- you can test whether something is an element of a vector with function is.element
or %in%
3) concatenating strings
- character strings can be concatenated with the function paste
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.