You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vectorized logical operations and logical indexes are extremely useful to compare, find, replace data elements.
218
218
219
+
Write functions in `R` to turn repeated, complex, or parameterized logic into reusable, modular, and reliable building blocks. Eliminate repetition, abstraction, modularity, reproducibility, encapsulation, vectorized design, code clarity and readability.
220
+
221
+
Big computation job can be broken into many small jobs which can be executed by a sequences of R functions either in sequential or in parallel.
222
+
223
+
Before any real computation, should use some procedures to check inputs: checking data type, data coercing, logical testing, etc.
224
+
219
225
- Domain: arguments lists
220
226
- Function body: expressions that define the function
221
227
- Range: return values
228
+
229
+
If the end of the function body is not an explicit `return` statement, the value of the last evaluated expression is returned.
230
+
231
+
### Variable Number of Arguments
232
+
233
+
-`...` in a function argument list is used to specify that an arbitrary number of arguments are to be passed to a function within the body of the function.
234
+
-`list(...)` to capture the variable arguments as a list
235
+
236
+
### Required and Optional Arguments
237
+
238
+
- Required arguments are those arguments for which the function definition provides neither a default value nor instructions on what to do if the argument is missing. All other arguments are optional.
239
+
240
+
### Testing Argument Lists
241
+
242
+
-`is.type(x)` to check if an argument is of a specific type, `is.vector`, `is.matrix`, `is.data.frame`, `is.integer`, `is.double`, `is.character`
243
+
-`as.type(x)` to convert an argument to a specific type, `as.vector`, `as.matrix`, `as.data.frame`, `as.integer`, `as.double`, `as.character`
244
+
245
+
### Logical Operators
246
+
247
+
-`&` vectorized AND
248
+
-`|` vectorized OR
249
+
-`&&` control flow AND
250
+
-`||` control flow OR
251
+
252
+
### Error Handling
253
+
254
+
-`stop("error message")` to throw an error and stop execution
255
+
-`warning("warning message")` to throw a warning and continue execution
256
+
-`try(expr)` to execute an expression and catch any errors without stopping execution
257
+
-`tryCatch(expr, error = function(e) { ... })` to execute an expression and handle errors with a custom function
0 commit comments