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
MATLAB
object-oriented, imperative style*
R
object-oriented, mixed style
Python
object-oriented, object-oriented style
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/type = abstraction
object = realization
vector is a class
c(1,2,3) is an object
classes are the idea
objects are examples of classes
is.vector()
Two Types:
homogeneous
is.atomic()
vectors, matrices, arrays
same principles as arrays in matlab
is.locial, is.character
<- 10:20
my.vec print(my.vec)
is.atomic(my.vec)
is.vector(my.vec)
dim(my.vector)
length(my.vector)
# period over underscore
<- seq(from = 0, to = 100, by = 20)
my.seq print(my.seq)
<- c(50, 20, 30, 50)
my.vec2 print(my.vec)
sort(my.vec2)
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
my.vec[tail(my.vec,n=1) # NOTE argument equals sign
-length(my.vec)]
my.vec[-tail(my.vec,n=4)] #all but last foour characters
my.vec[head(my.vec,n=2)] #same
my.vec[
<- rep(c(1,2,3), each = 3)
my.rep
2 %in% my.vec
2 %in% my.rep
match(2, my.rep)
match(c
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)
...
<- i
my.complex <- 8+3i
my.complex <- 8+i0
my.complex is.numeric(my.complex)
is.logical(my.complex)
typeof(my.complex)
typeof( c(8,8L) )
typeof( c(8,8i) )
More complex data types dominate
heterogeneous
Like cells in matlab. Avoid if possible.
<- list("my string",3,2)
my.list 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)
1]] # returns element of list
my.list[[1] # returns sub-list
my.list[1][1] # returns sub-sub-list
my.list[1:3]
my.list[unlist(my.list[c(2,3)])
<- 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
my.list[print(my.list)
1]] <- NULL # same if element has length of 1
my.list[[print(my.list)
for (x in my.list) {
print(my.list)
}
<- c(my.list,my.list)
my.rep.list print(my.rep.list)
In R, everything is an object including code.
<- function(x) {
my.rms <-x-mean(x)
dreturn(sqrt(mean(d^2)))
}my.rms(c(1,2,3))
Many times 'return' is optional
<- function(x,y) {
my.rms <-x-mean(x)
dsqrt(mean(d^2)))
}
<-my.rms(c(1,2,3))
Aprint(A)
Last evaluated statement is returned.
Don't assign it to anything!
<- function(x,y) {
my.rms <-x-mean(x)
d<-sqrt(mean(d^2)))
out
}
<-my.rms(c(1,2,3))
Aprint(A)
Uses
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
In R, everything is an object including code
call - individual statements as objects
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
R has the following primitive types or mdoes:
Classes are built on top of modes, adding additional structure
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 inherit variables from their glo
NULL data tyep is used in
Even Code statements are objects in R.