R Reference
Basic Operation
# this is a comment in R- Use
x <- 3to assign a value,3, to a variable,x - R counts from 1, unlike many other programming languages (e.g., Python)
length(thing)produces the length of a collectionc(value1, value2, value3)creates a vectorcontainer[i]selects the i’th element from the container
List objects in current environment ls()
Remove objects in current environment rm(x)
Remove all objects from current environment rm(list = ls())
Control Flow
Create a contitional using
if,elif, andelseif(x > 0){ print("value is positive") } else if (x < 0){ print("value is negative") } else{ print("value is neither positive nor negative") }create a
forloop to process elements in a collection one at a timefor (i in 1:5) { print(i) }
This will print:
1
2
3
4
5
- Use
==to test for equality 3 == 3, will returnTRUE,'apple' == 'orange'will returnFALSEX & YisTrueis both X and Y are trueX | YisTrueif either X or Y, or both are true
Functions
Defining a function:
is_positive <- function(integer_value){ if(interver_value > 0){ TRUE else{ FALSE { }
In R, the last executed line of a function is automatically returned
Specifying a default value for a function argrment
increment_me <- function(value_to_increment, value_to_increment_by = 1){ value_to_increment + value_to_increment_by }
increment_me(4), will return 5
intrement_me(4, 6), will return 10
Call a function by using
function_name(function_arguments)apply family of functions:
apply() sapply() lapply() mapply()
apply(dat, MARGIN = 2, mean) will return the average (mean) of each column in dat
Packages
- Install package by using
install.packages("package-name") - Update packages by using
update.packages("package-name") - Load packages by using
library("package-name")