############################################################################### ### Introduction to R ### see also www.cran.r-project.org/doc/manuals/R-intro.pdf ############################################################################### ###---------------------------------------------------------------------------- ### Basic operations ###---------------------------------------------------------------------------- ## Scaler operation ## i have a dream a <- 3 b <- 4 c <- a+b d <- a^2+b e <- a/b f <- a^b a*b class(a) # check type of the data ## Vectors ## In R, a vector does not have dimension attributed x <- c(10.4, 5.6, 3.1, 6.4, 21.7) x^2 x + 4 x*2 x/3 exp(x) leght(x) # length of x ## Random numbers A <- rnorm(n = 100, mean = 0, sd = 1) mean(A) sd(A) B <- runif(n = 100, max = 1,min = 0) mean(B) sd(B) ## Matrics Z <- matrix(runif(24), 4, 6) Z dim(Z) nrow(Z) ncol(Z) length(Z) Z^2 D <- log(Z) D dim(D) Z + D D-D Z*D cbind(1, Z) # column-bind two vectors or matrices rbind(1, Z) # row-bind TZ <- t(Z) # transpose of Z, 6x4 matrix TZ dim(TZ) Q <- Z %*% TZ # matrix multiplication, 4x4 dim(Q) all.equal(t(Q), Q) # is Q symmetric? solve(Q) # inverse of Q ## Indices for vectors x[1] x[-1] x[-c(2, 4)] x[2:4] x[c(1, 3, 4)] x[length(x)] ## Indices for matrices K <- Z[, 1] # extract the first column, don't forget the comma Z[1, ] # extract the first row M <- Z[, 1:3] Z[2:4, ] dim(M) dim(K) # the dimension will be dropped if only one row or column is selected K1 <- Z[, 1, drop = FALSE] # prevent dropping dimensions dim(K1) D[1:3] # what will get for this? ## Help in R ?matrix # get help for a function help("matrix") example(matrix) # learn how to use the function. ###---------------------------------------------------------------------------- ### The working environment ###---------------------------------------------------------------------------- ls() # list all user created variables in R A rm(A) # remove variable "A" in R rm(list = ls()) # remove all user created variables in R getwd() # your current working directory setwd("") # set your working directory getwd() # check your working directory again save.image(file = "myRWorkSpace.Rdata") # Save all the variables into your hard disk q() # quit R # start R again load(file = "myRWorkSpace.Rdata") # Load your previous working space into R ###---------------------------------------------------------------------------- ### Import data into R ###---------------------------------------------------------------------------- ## clean your data before you input it ## Put your data in the same folder as your working directory ## R can only read txt, csv format data. ## Keep your data file name simple(without space) DataRaw <- read.table("Table_1_1.csv", header = TRUE) DataRaw Y <- DataRaw[, 2, drop = TRUE] # obtain Y X1 <- DataRaw[, 4] X2 <- DataRaw[, 5] X <- cbind(1, X1, X2) # add an intercept ###---------------------------------------------------------------------------- ### Simple plot ###---------------------------------------------------------------------------- ## Scatter plot plot(X1, Y) plot(X1, Y, col = "blue") plot(X1, Y, xlab = "This X", ylab = "This Y", main = "My first R plot") ## Histogram hist(Y) hist(Y, breaks = 20) hist(Y, breaks = 20, freq = FALSE) ## Boxplot boxplot(X1) boxplot(cbind(X1, X2)) # boxplot of two matrices