#display the names of (most of) the objects which are currently stored within R objects() ls() #remove objects rm(x, y, z, ink, junk, temp, foo, bar)
All objects created during an R session can be stored permanently in a file for use in future R sessions. At the end of each R session you are given the opportunity to save all the currently available objects. If you indicate that you want to do this, the objects are written to a file called ‘.RData’5 in the current directory, and the command lines used in the session are saved to a file called ‘.Rhistory’.
# 3 way of assignment x <- c(10.4, 5.6, 3.1, 6.4, 21.7) c(10.4, 5.6, 3.1, 6.4, 21.7) -> x assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7)) #create vector by vector y <- c(x, 0, x)
regular sequences
1:15 # got 1,2,3,4...15 2*1:10 # got 2,4,6,8,10,12,...20 seq(1,10) # got 1,2,3,4,5...10 seq(-5, 5, by=.2) -> s3 # got c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0). rep(x,times=2) # got c(1,2,3,4,1,2,3,4) rep(x,each=2) # got c(1,1,2,2,3,3,4,4)
TRUE FALSE NA are logical vector , Logical vectors are generated by conditions
Vectors must have their values all of the same mode.
temp <- x > 13 # got TRUE or FALSE z <- c(1:3,NA); ind <- is.na(z) # got c(FALSE,FALSE,FALSE,TRUE)
Model
logical, numeric, complex, character or raw.
c("1",1) # got c("1","1") charactor mode #change mode z <- 1:3 digits <- as.character(z) # got c("1","2","3") as.integer(digits) # got 1:3
empty vector
numeric(0) # model numeric character(0) # mode character
Length
list,function and expression
attributes(x) # got all attributes attr(x,"name") # got attribute by name attr(x,"name")<-value # set attribute value on name
create n-dimension array from vector
a = 1:100 dim(a)<-c(10,5,2) # got a 3-dimension vector # a must have the same length of 10*5*2 in this case a[1,1,1] # index 0 a[10,1,1] # index 9 a[10,5,1] # index 49 a[10,5,2] # index 99
create 2-dimension array directly
arr <-array(1:20,dim=c(4,5))
Negative indices are not allowed in index matrices. NA and zero values are allowed
#if h is shorter than 24, its values are recycled from the beginning again to make it up to size 24 vec = 1:10 arr = array(vec,dim=c(4*3)) # got # 1 5 9 # 2 6 10 # 3 7 1 # 4 8 2
outer product
ab <- a %o% b outer(a,b,"*") # "*" can be replaced by function f <- function(x, y) cos(y)/(1 + x^2) z <- outer(x, y, f)
transpose of A
t(x) aperm(x, c(2,1))
矩阵乘法
x %*% y
diag(p) , if p is diagnal entries get a matrix , if p is matrix get a diagonal entries
diag(v) # get a diagonal matrix diag(M) # get the main diagonal entries
b <- A %*% x solve(A,b) eigenvalues # 特征值 eigenvectors # 特征向量 symmetric #对称
Eigenvalues and eigenvectors
ev <- eigen(Sm) evals <- eigen(Sm)$values evals <- eigen(Sm)$vectors evals <- eigen(Sm, only.values = TRUE)$values evals <- eigen(Sm, only.values = TRUE)$values
Singular value decomposition & determinants
orthonormal
Lst <- list(name="Fred", wife="Mary", no.children=3, child.ages=c(4,7,9)) Lst[1] Lst[2] Lst[3] Lst[4] Lst$name Lst$wife Lst$no.children Lst$child.ages Lst[["name"]] Lst[["wife"]] Lst[["no.children"]] Lst[["child.ages"]]
A data frame is a list with class "data.frame".
Another frequent requirement is to allow one function to pass on argument settings to another. For example many graphics functions use the function par() and functions like plot() allow the user to pass on graphical parameters to par() to control the graphical output.
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) { [omitted statements] if (graph) par(pch="*", ...) [more omissions] }