R Part 2 - Modes: the primitive data types

Dave White

Data-types

matlab - everything array w/ element datatype
R - underlying datatype with some structure

mode = underlying datatype
class = underlying structure

matlab - start with array, fill in underlying type
R - start with underlying type, build structure on top

Implementation vs style

MATLAB
object-oriented, imperative style*
R
object-oriented, mixed style
Python
object-oriented, object-oriented style

R Data types

In R Modes are "primitive" types.
Classes are built from modes, adding additional structure.
Without any additional class structure, the class and mode are equivalent.

mode mode()
class class()
dimensiosn dim()
length* length()
recursion is.recursive()
heterogeneity is.list()

class vs object

class/type = abstraction
object = realization

vector is a class
c(1,2,3) is an object

classes are the idea
objects are examples of classes

Basic Types/Modes

Vectors

is.vector()
Two Types:

Atomic Vectors

homogeneous
is.atomic()
vectors, matrices, arrays
same principles as arrays in matlab

Atomic vector modes

is.locial, is.character

Atomic vectors

my.vec  <- 10:20
print(my.vec)
is.atomic(my.vec)
is.vector(my.vec)
dim(my.vector)
length(my.vector)

# period over underscore

my.seq <- seq(from = 0, to = 100, by = 20)
print(my.seq)

my.vec2 <- c(50, 20, 30, 50)
print(my.vec)
sort(my.vec2)

my.vec[2]
my.vec[c(2,4)]
my.vec[-1] # negative signs are compliment, all but last char
my.vec[c(2,4)] # negative signs are compliment, all but last char
my.vec[length(my.vec)] # negative signs are compliment, all but last char
tail(my.vec,n=1) # NOTE argument equals sign

my.vec[-length(my.vec)]
my.vec[-tail(my.vec,n=4)] #all but last foour characters
my.vec[head(my.vec,n=2)] #same

my.rep <- rep(c(1,2,3), each = 3)

2 %in% my.vec
2 %in% my.rep

match(2, my.rep)
match(c

Integer Logical

Integer and Logical are sub-modes aka storage.modes

my.int <- (8L,7L)
is.numeric(my.int)
is.vector(my.int)
is.isatomic(my.int)
mode.storage(my.int)


# LOGICAL
my.logic <- c(TRUE,FALSE)
is.numeric(my.logic)
is.logical(my.logic)
mode.storage(my.int)
mode.storage(my.int)
...
my.complex <- i
my.complex <- 8+3i
my.complex <- 8+i0
is.numeric(my.complex)
is.logical(my.complex)
typeof(my.complex)

typeof( c(8,8L) )
typeof( c(8,8i) )

More complex data types dominate

  1. complex
  2. double
  3. integer
  4. logical

Atomic vector classes

List

heterogeneous
Like cells in matlab. Avoid if possible.

my.list <- list("my string",3,2)
print(my.list)
print(length(my.list))
"my string" %in% my.list

append(my.list, "a 2nd string") # no assignment?
print(my.list)
cat(my.list) # should return error
append(my.list, "a 3rd string", after = 2)

my.list[[1]] # returns element of list
my.list[1] # returns sub-list
my.list[1][1] # returns sub-sub-list
my.list[1:3]
unlist(my.list[c(2,3)])

my.list <- list(1:5) # vector becomes first element
my.list <- as.list(1:5)
my.list <- my.list[-1] # remove first element & shift
my.list[1] <- NULL  # same
print(my.list)
my.list[[1]] <- NULL  # same if element has length of 1
print(my.list)

for (x in my.list) {
  print(my.list)
}

my.rep.list <- c(my.list,my.list)
print(my.rep.list)

List classes

Expressions

Functions

In R, everything is an object including code.

Basics

my.rms <- function(x) {
     d<-x-mean(x)
     return(sqrt(mean(d^2)))
}
my.rms(c(1,2,3))

Optional return symbol

Many times 'return' is optional

my.rms <- function(x,y) {
     d<-x-mean(x)
    sqrt(mean(d^2)))
}

A<-my.rms(c(1,2,3))
print(A)

Last evaluated statement is returned.
Don't assign it to anything!

my.rms <- function(x,y) {
     d<-x-mean(x)
     out<-sqrt(mean(d^2)))
}

A<-my.rms(c(1,2,3))
print(A)

NULL

Uses

  1. remove elements from vectors
  2. functions with no returns
  3. represent no data: without length or dimensions

compare with NA, which acts as a filler

A<-NA
B<-NULL
length(A)
length(B)
mode(A)
mode(B)


A<-c(NA,NA,NA)
B<-c(NULL,NULL,NULL)
length(A)
length(B)

A<-c(3,3,NA)
B<-c(3,3,NULL)
print(A)
mode(A)
print(B)
mode(B)
length(A)
length(B)

NA is a value in atomic vectors, not a type
NULL is its own type, with only a single value

Calls

In R, everything is an object including code

call - individual statements as objects

Call details

Using calls like data is meant for advanced programming called meta-programming
Here, we just want to highlight the fact that statements are objects in R.

round(3)
my.call<-call("round",3)
eval(my.call)
my.call <- call("abs",call("round",-3.1))
is.expression(my.call)
my.expression <- expression(abs(round(-3.1)))
eval(my.expression)
eval(my.expression[1])
is.atomic(my.expression)
is.list(my.expression)
is.vector(my.expression)

expressions are also vectors

Summary

R has the following primitive types or mdoes:

Classes are built on top of modes, adding additional structure

Vectors

Vectors occur in three types:

Atomic vectors occur in 5 types

Numeric Atomic Vectors have 2 storage types

All specifics of a vector mode need to be defined for it to exist
e.g.
A vector cannot be just a vector, it has to be atomic, list, or an expression
An atomic vector can't just be numeric, it has to also be double or integer

Atomic vectors are homogenous non-recursive.
Lists are heterogeneous and recursive.

Functions

Functions inherit variables from their glo

NULL

NULL data tyep is used in

  1. removing elements from vectors
  2. functions with no returns
  3. representing empty data, without length or dimensions

Calls

Even Code statements are objects in R.