1.35k likes | 1.57k Views
Math2411. Introduction to R. Introduction to R. R is a free statistical programming language. Download at: http://www.r-project.org. Or, simply google [R], it should appear as the first hit. Introduction to R. R is a free statistical programming language.
E N D
Math2411 Introduction to R Part II: Statistics
Introduction to R R is a free statistical programming language. Download at: http://www.r-project.org. Or, simply google [R], it should appear as the first hit Part II: Statistics
Introduction to R R is a free statistical programming language. Download at: http://www.r-project.org. Open R by double-clicking your desktop Icon : Part II: Statistics
Create a vector in R Part II: Statistics
Use a function c() “c” stands for concatenation most primitive way to create a column vector e.g. > c(1, 2, 3)numeric data [1] 1 2 3 - Integers > c(‘a’, ‘b’) or c(“a”, “b”) nonnumericdata [1] “a” “b” - Characters Creating Vectors Part II: Statistics
Creating Vectors Q : I want to use the same vector again… A : Give it a name and store it as a variable! Part II: Statistics
Assignment operator <- OR = e.g. > myvect <- c(1, 2, 3, 4, 5, 6, 7) OR > myvect = c(1, 2, 3, 4, 5, 6, 7) Creating Vectors Part II: Statistics
Now recall the variable myvect > myvect [1] 1 2 3 4 5 6 7 *Variable name starts with letters contains letters, numeric and period “.” only CASE SENSITIVE! Creating Vectors • Use t() to convert a column vector to a row vector For example: t(myvect) Part II: Statistics
Creating Vectors The function c() can also link two vectors together to form a new vector.
Use a function :to create a sequence x:y starts from x, increases by 1 up to y e.g. > myvect2 = 8:14 > myvect2 [1] 8 9 10 11 12 13 14 Step size = 1 Creating Vectors Part II: Statistics
Use a function seq() to create a sequence in more flexible way seq(x, y, k) Starts from x Increase by k up to y e.g. > myvect3 = seq(1, 14, 2) > myvect3 [1] 1 3 5 7 9 11 13 Step size = 2 Creating Vectors Part II: Statistics
“rep” stands for replicate rep(x, b) A vector consists of b replicates of x e.g. > rep(0, 5) [1] 0 0 0 0 0 > rep( c(1, 2), 2) [1] 1 2 1 2 > rep(‘a’, 3) [1] “a” “a” “a” Creating Vectors Part II: Statistics
Vector manipulation Part II: Statistics
Vector manipulation The square brackets [ ] are most commonly used e.g. To select the 2nd element in combine >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[2] [1] 9 Part II: Statistics
e.g. To select from the 2nd up to the 5th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[2:5] [1] 9 10 11 12 Vector manipulation Part II: Statistics
e.g. To drop the 3rd element >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ -3 ] [1] 8 9 11 12 13 14 1 2 3 4 5 6 7 Vector manipulation Part II: Statistics
e.g. To select from the 1st, 5th and then 4th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ c(1, 5, 4) ] [1] 8 12 11 Vector manipulation Part II: Statistics
e.g. To drop the 2nd, 3rd and then 5th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ -c(2, 3, 5) ] [1] 8 11 13 14 1 2 3 4 5 6 7 Vector manipulation Part II: Statistics
Basic math functions 2 + 3 # addition 2 – 3 # substraction 2 * 3 # multiplication 2 / 3 # division 2 ^ 3 # power log( 5 ) # natural logarithms sqrt( 2 ) # square root exp( 0.2 ) # exponential function abs( -7 ) # absolute value Part II: Statistics
Basic math functions a) Sum of a vector and a scalar x = c(11,8,3) x + 7 ? Each element of x will be added to 7 to form a new vector. x+7 (18, 15, 10) Part II: Statistics
Basic math functions b) Multiplication of a vector by a scalar x = c(11,8,3) x*7 ? Each element of x will be multiplied by 7 to form a new vector. x*7 (77, 56, 21) c) Division of a vector by a scalar x/7 (11/7, 8/7, 3/7)
d) Power Similarly, when applied to a vector, the values are evaluated element-wisely. e.g. >myvect [1] 1 2 3 4 5 6 7 >myvect^2 [1] 1 4 9 16 25 36 49 Basic math functions Part II: Statistics
Basic math functions We can also add, subtract, or multiply two or more vectors of the same length (a) Vector Sum (sum elementwisely) x = c(11,8,3), and y = c(2,6,3) x + y (11+2, 8+6, 3+3)=(13, 14, 6) (b) Vector Subtraction x - y (11-2, 8-6, 3-3)=(9, 2, 0) Part II: Statistics
Basic math functions Caution! Multiplying Vectors Part II: Statistics
t(x)%*%y = sum(x*y) Basic math functions c1) Vector Multiplication (elementwisely) x = c(11,8,3), and y = c(2,6,3) x * y (11*2, 8*6, 3*3) c2) Vector Multiplication (usual),i.e. xTy t(x)%*%y11*2+8*6+3*3
Basic math functions d) Vector Division (elementwisely) x = c(11,8,3), and y = c(2,6,3) x/y (11/2, 8/6, 3/3) Part II: Statistics
Create a matrix and matrix manipulation Part II: Statistics
?matrix Or help(matrix) Create a Matrix Use matrix() Part II: Statistics
?matrix Or help(matrix) x = matrix(c(3,5,6,3,4,9,2,1,7), ncol=3, nrow=3, byrow=T) The matrix is filled by rows. Create a Matrix Use matrix() Part II: Statistics
1st 2nd 3rd column The (1,2)th element of x 1st row x[1,2]: the element in the 1st rowAND2nd column 2nd row 3rd row x[,1] x[3,] The 1st column vector The 3rd row vector Part II: Statistics
Matrix Manipulation x[-i, ]: drop the ith row vector of x. x[-c(i,j), ]: drop the ith and jth row vectors of x. x[ ,-i]: drop the ith column vector of x. x[ ,c(i,j)]: keep the ith and jth column vectors of x. Part II: Statistics