```{r setup, include=FALSE} library(methods) knitr::opts_chunk$set(echo = TRUE, comment = "") ``` # R Basics ## Data Classes - Numeric - numbers (e.g. 1, 3.673) - Character - strings or words (`"hey"`, `"I'm a string"`) in either single or double quotes - Logicals - `TRUE` or `FALSE` - all capital letters and are **not** in quotes. ## Data Types - `vector` - 1-dimensional object of one class (all numeric or all character) - `matrix` - 2-dimensional object of one class - `data.frame` - 2-dimensional object, can be multiple classes (like Excel spreadsheet) - `array` - object of > 2 dimensions of one class. The data in a `nifti` object is one of these (usually 3-D) ## Initializing: vectors - Use `c()` to create a vector of numeric values: ```{r vec} v = c(1, 4, 3, 7, 8) print(v) ``` - Shortcut for sequential numeric vector: ```{r vec2} w = 1:5 print(w) ``` The gray boxes denote code, and the lines after denote the output. ## Assignment In `R`, you can assign using the equals `=` or arrow `<-` (aka assigment operator). The above commands are equivalent to: ```{r, eval = FALSE} w = 1:5 w <- 1:5 ``` There are no differences in these 2 commands, but just preference (we use `=`). Variable/object names: - must start with a letter - cannot contain spaces, `$`, quotes, or other special characters - generally just alpha-numeric - **can** contain periods (`.`) and underscores `_` ## Help - To see the documentation for a function, use the `?` symbol before the name of the function. This is a shortcut for the `help` command: - For example, to see documentation for `c`: ```{r help, eval = FALSE} ?c help(topic = "c") ``` - To search for help files, use a double `??` or `help.search`: ```{r help2, eval = FALSE} ??c help.search(pattern = "c") ``` ## Some Details - R is case sensitive (e.g. `y` and `Y` are different) - Commands separated by new line or by a colon (;) - Use `#` to comment You can also be explicit about which package you are using with the `::` operator, where the syntax is `package::function()`: ```{r colon_twice, eval=FALSE} utils::help("c") ``` ## Initializing: matrices and arrays - Create a 3 x 4 numeric matrix and assign to variable `m` - note items are added column-wise ```{r mat} m = matrix(1:12, nrow = 3) print(m) ``` - Create a 3 x 4 x 3 numeric array and assign to variable `a` ```{r arr} a = array(1:36, dim = c(3, 4, 3)) ``` - the `dim()` function returns the dimensions of the array ```{r dim} dim(a) ``` ## Subsetting: vectors - Subsetting a vector (first index is `1`, not zero): ```{r vecSub} print(v) print(v[4]) print(v[1:3]) print(v[c(1,3,5)]) ``` ## Subsetting: matrices - Subsetting a matrix - `[row,column]` format, ```{r matSub} print(m[1,3]) print(m[1:2,3:4]) ``` - if `row` or `column` missing then all values printed: ```{r matsub2} print(m[,4]) print(m[2,]) ``` ## Subsetting: arrays - Subsetting - `[x,y,z]` format: ```{r arrSub} print(a[1,1,1]) dim(a[,4,]) ``` This will return an error - need to specify all dims: ```{r arrSub_bad, eval = FALSE} a[,4] ``` ## Operators in R: return numeric - Arithmetic: `+`, `-`, `*`, `/`, `^` - exponents - Standard math functions: `log`, `abs`, `sqrt` ```{r add_vw} print(v); print(w) print(v + 4) print(v + w) print(sqrt(w^2)) ``` ## Operators in R: return logical - Comparison: `>`, `>=`, `<`, `<=`, `==` (equals), `!=` (not equal) - Logical: `!` - not, `&` - and, `|` - or (a "pipe") - `all()`: function to test all values `TRUE` and `any()`: (are any) ```{r true} print(!FALSE) print(TRUE | FALSE) print(FALSE & FALSE) c(all(c(TRUE, FALSE)), any(c(TRUE, FALSE))) ``` ## Subsetting with logicals The `which` command takes a logical and gets the indices of `TRUE`: ```{r} which(v > 5) v[ which(v > 5) ] ``` Or directly pass in a vector of logicals to subset: ```{r} v[ v > 5 ] ``` This method will be useful later when we are working with images. ## Website http://johnmuschelli.com/imaging_in_r