Lecture 10 - Programming Basics
In this lecture note, we will delve into the core of R programming by exploring its building blocks. Think of R as a language: to communicate effectively, you need to know the grammar (syntax), vocabulary (functions), and the rules of conversation (flow control and loops). Let us unpack these one by one.
R Syntax
Core Apply Functions
| Function | Input | Output | Description |
|---|---|---|---|
apply(x, margin, FUN) |
Mutlidimension (dim) |
Customizable | Applies a function over rows or columns of a matrix or dimensions of an array. |
lapply(x, FUN) |
List-type (as.list) |
List | Applies a function to each element of a list or vector and returns a list. |
sapply(x, FUN) |
List-type (as.list) |
Simplified | Like lapply, but tries to simplify output. |
vapply(x, FUN, FUN.VALUE) |
List-type (as.list) |
User-defined | Like sapply, but enforces a specific return type. |