N = 5 # simple variable N equal to 5 X <- c(1,5,6) #make a vector x with values of 1,5,6 Y <- 1:10 #a column vector Y <- c() # empty vector #simple operation data <- 2*X; #all elements of X gets doubled #define matrix m = matrix(c(1:40), nrow=4, ncol=10, byrow=T); [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 11 12 13 14 15 16 17 18 19 20 [3,] 21 22 23 24 25 26 27 28 29 30 [4,] 31 32 33 34 35 36 37 38 39 40 m = matrix(c(1:40), nrow=4, ncol=10, byrow=F); [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 5 9 13 17 21 25 29 33 37 [2,] 2 6 10 14 18 22 26 30 34 38 [3,] 3 7 11 15 19 23 27 31 35 39 [4,] 4 8 12 16 20 24 28 32 36 40 m = matrix(c(0), nrow=4, ncol=10, byrow=F); #matrix of zeros m = matrix(c(1), nrow=4, ncol=10, byrow=F); #matrix of ones m = diag(10) #diagonal matrix m = 10*diag(10) #diagonal matrix with 10 m = rnorm(1000,0,1) #generate 1000 random number from normal distribution with mean of zero and variance of 1 m = rnorm(1000000,0,1) mean(m) #mean of m sum(m) #sum of m max(a) sort(a) var(m) #variance of m ls() #list all the variable in this session m = (m -mean(m))/sd(m) #make sure the mean and variance are normal rm(X) #remove a variable name X Error: object 'X' not found write.table(v, file="v.tab") # write v into v.tab file save(v, file="v.binary") # write v into binary file v.binary file v=read.table("v.tab") # read v table from v.tab load("v.binary") # load v from the binary file v.binary # How to access matrix elements m = matrix(c(1:40), nrow=4, ncol=10, byrow=F);#matrix of zeros m[1,3] m[,3] # all the elements in the third column m[1,] # all the elements in the first row #Simple elements operator a = c(1,2,3,4) # vector 2 * a # scalar multiplication a / 4 # scalar multiplication b = c(5,6,7,8); # vector a + b # pointwise vector addition a - b # pointwise vector addition a ^ 2 # pointise vector squaring a * b # pointwise vector multiply a / b # pointwise vector divide log( c(1,2,3,4) ) # pointwise arithmetic operation round( matrix(c(1,2,3,4,5,6), nrow=2, ncol=3)) # pointwise arithmetic operation #matrix operation m = matrix(c(1:40), nrow=4, ncol=10, byrow=T) dim(m) #row and column of matrix dim(m)[1] # 4 row dim(m)[2] # 10 column dim(m) <- c(1,40) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 1 11 21 31 2 12 22 32 3 13 23 33 4 14 [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [1,] 24 34 5 15 25 35 6 16 26 36 7 17 [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [1,] 27 37 8 18 28 38 9 19 29 39 10 20 [,39] [,40] [1,] 30 40 t(m) # transpose of a matrix #Saving your work save.session("RSession.Rda") # creates mysession.mat with all variables rm(list=ls()) # clear all variables rm(a, b) # clear variables a and b load.session("RSession.Rda") # load session #Plotting x = c(0, 1, 2, 3, 4); # basic plotting plot( x ); plot( x, 2*x ); x = pi*(-24:24)/24; plot( x, sin(x), xlab="radians", ylab="sin value", main="dummy"); # multiple functions in separate graphs par(mfrow=c(1,2)) plot( x, sin(x) ); plot( x, 2.*cos(x) ); par(mfrow=c(1,3)) plot( x, sin(x) ); plot( x, 2.*cos(x) ); plot( x, sin(x)+2*cos(x) ); # multiple functions in single graph plot( x,sin(x) ); # hold on tells matlab to write on top lines (x, cos(x)); # of the current plot legend(-3, 1, c("cos","sin"), cex=0.8, pch= c(-1, 1),lty=c(1, 0)); # string concatenation tmp = "is" str = paste("This",tmp,"a test", sep=" ") print(str) # set working directory setwd("~/Desktop/") # function square <- function(x){ x2 <- x^2 return(x2) } square(x = 2) #loops for (i in 1:10) print(i) add <- function(x, y) x + y sapply(1:10, add, 3) [1] 4 5 6 7 8 9 10 11 12 13 #draw Figures res = cbind( c(0.001,0.0003), c(0.0051, 0.0023), c(0.0092, 0.0028), c(0.0128, 0.0059), c(0.0134, 0.0077), c(0.0236,0.0149),c(0.026, 0.018), c(0.0218, 0.0166)); colnames(res) = colnames(res) = c("2","3","4", "5", "6", "10", "20", "30"); barplot(res, main="", cex.names=1.6 , cex.axis=1.6,col=c("darkgrey","black"), beside=TRUE, legend = c("CS","eALPS"), args.legend = list(cex=1.6 , x = "topleft")) title(ylab = "RMSE",cex.lab=1.6) title(xlab = "non-zero bacteria",cex.lab=1.6) res = rbind( c(0.001,0.0003), c(0.0051, 0.0023), c(0.0092, 0.0028), c(0.0128, 0.0059), c(0.0134, 0.0077), c(0.0236,0.0149),c(0.026, 0.018), c(0.0218, 0.0166)); [,1] [,2] [1,] 0.0010 0.0003 [2,] 0.0051 0.0023 [3,] 0.0092 0.0028 [4,] 0.0128 0.0059 [5,] 0.0134 0.0077 [6,] 0.0236 0.0149 [7,] 0.0260 0.0180 [8,] 0.0218 0.0166 ##how to check our statistics follows normal distribution data = rnorm(1000, 0,1) qqnorm(data) qqline(data,col="red")