R Flashcards
console
where to write R functions and code; doesn’t save the code you wrote
environment
where you see the objects you’ve created
order of operations in R
PEDMAS
print()
prints the value stored in an object
rules for naming identifiers in R
must start with letter or period; if it starts with a period, can’t be followed by a digit
reserved words can’t be used as identifiers
function
piece of code that performs a specific tasl
arguments should be listed within…
parentheses
data types in R
numeric: double or integer (L after the number)
string: character
logical: TRUE or FALSE
typeof()
displays the data type of the argument passed
alphabetical string comparison
how are they compared?
dictionary order; assume all in lowercase
if there is a tie when everything is assumed lowercase, lowercase < uppercase
if there is a number, digit < letter
numbers < lowercase letters < uppercase letters
why is TRUE + FALSE = 1?
TRUE is coerced to 1
FALSE is coerced to 0
therefore 1 + 0 = 1
implicit coercion
R converts data types to be able to accomplish commands
AND, OR, NOT
what are the symbols used?
&, |, !
when can you break a line in R?
after , & and %>%
atomic data
object that holds a single value
vector
can a vector have different data types? what if it’s NA?
object that holds multiple values of the same data type; like a column/row array
always the same data type, even if it’s NA
creating a vector
vectorname <- c(element1, element2, element3)
- TRUE&TRUE
- FALSE&TRUE
- TRUE&FALSE
- TRUE
- FALSE
- FALSE
creating a new vector that is numbers added to an old vector
newvector <- oldvector + 2
OR newvector <- oldvector +c(2, 2, 1, 1)
length(x)
x is an object
outputs the number of elements in X
subsetting
how are indices numbered?
retrieving specific elements from a vector using indices
numbered starting from 1
- retrieve an element from a vector with a given index
- retrieve a range of indices
- retrieve specific elements with specific indices
- retrieve all but index 3
- retrieve all but index 2 and 3
- vectorname[index]
- vectorname[index:index]
- vectorname[c(1, 2, 4)]
- vectorname[-3]
- vectorname[-c(2,3)]
adding new elements to an existing vector
vectorname[3:4] <- c(“newvalue1”,”newvalue2”)
assign to locations with no values within an existing vector
which(x)
what is x?
gives indices of TRUEs; output is a vector of position numbers
used to identify particular observations that satisfy the condition specified
x is a logical vector
vectors
subsetting the entries that satisfy a condition
column[which(condition)]
ex. cities[which(population > 100000)]
max, min, range, sum, mean, sd, var, sqrt, sort
max value, min value, min and max values, sum, average, sd, variance, square root, puts elements in ascending order
coercion in vector creation
what is the order?
if elements of a vector are specified in different data types, R will coerce them into 1 data type
therefore typeof(vector elements) = highest-ordered data type
logical > integer > double > character