Objects and Operators

Objects in R

Object types: integer, numerical, character, factor, date, logical EDIT AND LOOK UP

Vector: one dimensional array; the elements all have the same type

my_vector <- c(1,2,3,4,5)
my_vector
[1] 1 2 3 4 5

Matrix: two dimensions of same type; ie matrix(c(1:9),byrow = TRUE, nrow = 3) )

my_matrix <- matrix(c(1,2,3,4,5,6,7,8,9), byrow = TRUE, nrow = 3)
my_matrix
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

or:
my_matrix <- matrix(c(1:9), byrow = TRUE, nrow = 3)
my_matrix
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

Data frame could contain different data types in different columns

List can contain combinations of dataframes, vectors other lists etc

Factors

to be edited

Operators in R

TO BE EDITED:

R operators:

Arithmetic operators

<-   =  (ie x<-5 define x as number 5)

+   addition (ie: 5+2=7)

–  subtraction (ie 5-2=3)

*   multiplication (ie 5*3=15)

/   division (ie 6/3=2)

^   exponent (ie 5^2=25)

x %% y   modulus (ie 5%%3 =2)

x %/% y integer division (ie 5%/%3=1)

Logical operators

<   Less than

<=   less than or equal to

>   more than

>=   more than or equal to

==   is equal to

!   not

&   and

|   or

isTRUE(x)   is x true?  (ie isTRUE(5<4),  FALSE)

is.na(x)   is ‘not available’ (NA) (ie x<-5, is.na(x), FALSE)

!(is.na(x))   is not ‘not available (NA) (ie x<-5, !(is.na(x)), TRUE)

ifelse(test, yes, no)  if test condition is true ‘yes’, otherwise ‘no’

Relational Operators

types and examples

Logical Operators

types and examples

Conditional Operators

types and examples