2 Flashcards
How to sample a vector in R?
sample(y, size, replace, prob=vector), replace = do i want to place the item back in there or leave it out so it cant be selected again.
Logical vectors?
A vector but with different elements than just numbers, such as booleans
Negation in R (true values into false values and vise aversa)?
!vector
And operator (return a true if both values are true)?
vector1 & vector2
Or operator (returns a true value if at least one of the values is true)?
vector1 | vector2
What happens if you take the inequality of two vectors?
It returns true or false depending on whether the inequality of those elements hold
all(z)?
Checks to see if all the elements are true in a logical vector?
any(vector)?
Return true if at least one of them is true
Filter elements using logical vectors?
vector[logical vector], filters out all the falses, or, vector[condition such as an inequality]
How to do an if else in one line?
ifelse(test, output, else)
Example
x = 1, 2, 3, 4, 5, 6, 7, 8
ifelse(x == 8, x, -x): -1, -2, -3, -4, -5, -6, -7, 8
Factor function in R?
factor(x, level = vector), level is telling the program that there were other possible values, but some of those values are missing from the actual vector.
Why is the factor function useful?
If you run table(factor(x)), then it counts how many times each value occurs, including the missing values.
& operator in R?
Compares the logical value of two entire vectors and returns true if the two elements are true.
If you have && or || when comparing logical vectors, this means?
Scaler; only comparing the first element.
Union of two vectors?
union(x, y)