Show Menu
Cheatography

r part3 Cheat Sheet (DRAFT) by

This is a Summary of the Lecture 3

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Attributes 1

attr()
to retrieve or set single attributes
struct­ure()
to set all the attributes in one shot
attrib­utes()
to list all the attributes in one shot
(**Basics)

Matrices 1

mat2<-matrix(c(1,2,3, TRUE, FALSE, FALSE),3,2)

typeof(mat2)

[1] "double"

+ Subsetting matrices
The element in 1 row and 3column:
mat[1,3]

Extract the second row:
mat[2, ]

Extract the second column:
mat[ ,2]

Erase the second column:
mat[,-2]

Select a subset:
mat[1:2,1:2]
(**Basics)

Matrices 2

t(A)
transposed
diag(A)
,
diag(c(.))
diagonal
solve(.)
inverse
matrix(1:6,2,3)+
matrix(6:1,2,3)
sum
matrix(1:6,2,3)-
matrix(6:1,2,3)
substract
matrix(1/(1:6),2,3)*
matrix(1:6,2,3)
product elem
matrix(1/(1:6),2,3)%*%
matrix(1:6,2,3)
product
crossprod(A,B)
cross-­product
outer(A,B)
outer product
rbind()
cbind()
bind by rows, by columns
(**Bas­ics­)(**­*A­dva­nced)
 

Attributes 2

+ Set two different attributes
x<-1:10

attr(x,"att1") <- "AttV"

attr(x,"att2") <- 14

+ Same with
structure
:

x <- structure(1:10, att1 = "AttV", att2 = 14)

+ Names:
names(x) <- c(.)

setNames(.,c(.))

+ Unnames:
unname(x)

names(x)<-NULL

+Identical?
identitcal(mat1,mat2)

+Yes, not the SAME
lobstr::obj_addr(mat1)

[1] "0xf3107a0"
lobstr::obj_addr(mat2)

[1] "0xf310610"
(**Basics)
 

Lists 1 - generic vector

(lis1 <- list(1:5, c("a","b"), 
 c(TRUE,FALSE,TRUE), 2.3))
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] "a" "b"
[[3]]
[1]  TRUE FALSE TRUE
[[4]] 
[1] 2.3
A list can contain lists.

Lists 2

typeof(lis1)
"­lis­t"
class(lis)
"­lis­t"
attributes(lis1)
NULL
is.object(lis1)
FALSE
is.list(lis1)
TRUE
str(lis1)
List of 4  
$ : int [1:5] 1 2 3 4 5
$ : chr [1:2] "a" "b"
$ : logi [1:3] TRUE FALSE TRUE
$ : num 2.3
 lobstr:ref() 
compare changes in the references
unlist(lis1)
"­1" "­2" "­3" "­a" "­b" "­1" "­2" "­3" "­4"
list_name[c(.)]

list_name[[.]]

list$name
Subset elements
(**Bas­ics­)(**­*A­dva­nced)

Lists 3: Preall­ocation vs Empty

vector­("li­st",5) vs list()

(***Ad­vanced)