R


Types

Numeric

integer

64

Character

Character

"A"

Logical

Boolean value (either true or false)

TRUE

Complex number

Complex number

2 + i

Arithmetic operators

+

Addition

Unary positive, makes number positive

-

Subtraction

Unary negative, makes number negative

*

Multiplication

/

Division

%/%

Modulo (remainder of division)

%%

Modulo (remainder of division)

Assignment operators

<-

Equals

Comparison operators

==

Is equal to

!=

Is not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

instanceof

Checks if an object is a certain type

Elementwise operators

&

Elementwise AND

|

Elementwise OR

Control operators

Logical operators

&&

Logical AND

||

Logical OR

!

Logical NOT

Miscellaneous operators

,

Used to separate parameters

[]

References a data structure's index (starting from 1, not 0)

list[3]

References a sub-data structure by passing a datastructure with boolean datavalues to determine which indexes are accepted in the sub-data structure

{}

Defines a block

:

Creates a vector of numbers in a sequence

1:5 # same as c(1,2,3,4,5) or seq(from=1,to=5,by=1)

%*%

Matrix multiplication

""

Declares a character

''

Declares a character

$

Accesses a variable in the list

list$names

~

Links an independent and dependent variable for regression

Statements

if

Executes code if condition is true

if (condition) {
 code
}

while

Loops code while a condition is true

while (condition) {
 code
}

for

Loops code for a set amount iterations or for every object in list

for (variable in array) {
 code
}

function

f <- function () {
 code
}

repeat

Loops code until error

repeat {
 code
}

source

Sets the R file to execute

sink

Pipes output to some file, or on no input, to the command line

is.na

Returns whether a value is NA

any

Returns true if any data value in the input is true

length

returns the length of a list

seq

dim

Converts vector to matrix

dim(vec) <- c(2,3) #This makes a 2*3 matrix

sample

Creates a vector filled with random data values from some range

sample(1:5,20,rep=T)

lm

Creates a linear model from two vectors of data

lm(data$x ~ data$y)

mod1 <- lm(x ~ y, data = dat)

read.csv

Returns data from a CSV file

dat < read.csv("~/path/to/dataset.csv")

summary

Summary of information on a linear model

summary(mod1)

plot

Creates a data plot

plot(x, y, xlab, ylab)

pt

Calculates the CDF for Student's T distribution up to some T-test statistic

pt(0.69, 198) # pt(statistic, df, lower.tail=)

pt

Calculates the CDF for Student's T distribution up to some T-test statistic

pchisq

Data structures

Vector

List of data values of the same type

Matrix

Two dimensional dataset with columns and rows

matrix(c(1,0,0,0,1,0,0,0,1), nrow=3, ncol=2)

Dataframe

List

Ordered and mutable List of data values

Hmisc

car

olsrr

mtcar

Commenting

#

Used to tell compiler the rest of this line is not to be compiled

#This isn't executed