Please enable JavaScript.
Coggle requires JavaScript to display documents.
Intermediate R, For loop (for 循环) (Loop version 2:
for (i in 1…
-
-
-
-
-
A break statement:
- is used inside a loop (repeat, for, while) to stop the iterations;
- and flow the control outside of the loop.
A next statement:
- is useful when we want to skip the current iteration of a loop without terminating it.
- On encountering next, the R parser skips further evaluation and starts next iteration of the loop.
-
-
-
-
require(), similar to library() but not showing error messages if package unavailable.
gsub()
- function replaces all matches of a string, if the parameter is a string vector, returns a string vector of the same length and with the same attributes (after possible coercion to character).
diff()
- Returns suitably lagged and iterated differences.
grep()
- search for matches to argument pattern within each element of a character vector
Seq() and Rep():
- rep(seq(1, 7, by = 2), times = 7)
- seq(from, to, by)
grepl(pattern = "string", x)
- returns logic of the match
grep(pattern = "string", x)
- vector of the indices of the elements of x that yielded a match.
- sub(pattern = "to be replaced", replacement = "replaced with", x);
- sub() function only moves the first match of the string;
- gsub(pattern, replacement, x);
- Sub() moves all the matches in the string;
- Logical Opertors & Vectors:
- Logical Operators (逻辑运算符):
- AND operator &
- OR operator |
- Not operator !
Counting total number of TRUE, Sum()
- Conditional and Control Flow
- else Statement
- if (condition)
{expr 1}
else
{expr 2}
- elseif Statement
- if (condition 1)
{expr 1}
else if (condition 2)
{expr 2}
else if (condition 3)
{expr 3}
else
{expr 4}
-
-
- Introduction to Functions
-
mean(x, trim = 0, na.rm = FALSE):
- trim=0.1, remove top and bottom 10 % of the population
- na.rm = True, remove missing values from the input vector.
my_fun <- function(arg1, arg2) {body}
-
Use lapply with additional arguments:
multiply <- function(x, factor) {x * factor}
lapply(list(1,2,3), multiply, factor = 3)
Apply a function over a list or vector, and returns a list.
-
vapply(X, FUN, FUN.VALUE, …, USE.NAMES = TRUE)
-
Equality == , to see if true
Inequality !=, to see if false
‘>’, Larger than
’<‘, Small than
-
Similar to lapply, but returns a vector, matrix or array.
-