Intro to R Lecture 2

33
CME/Stats 195 Lecture 2 Xiaotong Suo [email protected]

description

The second lecture of Intro to R programming

Transcript of Intro to R Lecture 2

  • CME/Stats 195 Lecture 2

    Xiaotong Suo [email protected]

  • More logis>cs

    Knowledge of some calculus Course enrollment Five candies rule Apologies for the coursework Oce hour?

  • Ques>ons from last lecture

    #: comment your code median(x=1:10) why it didnt work? How to load your scripts where you dene all the func>ons? source(lename.r)

  • Install packages

    There are many nice packages in R and some of them are not in default. You may want to install them for your research. install.packages(packagename) Then you need to type: library(packagename) in order to use package.

  • Arithme>c Operators

  • Logical operators

  • Introduc>on to R

    In order to work with a language we need to know the objects that language oers. R oers 5 basic objects: vector, matrix, factor, dataframe and list.

  • Todays Agenda

    Vector Matrix

  • Vector

    A vector is a collec>on of objects all of the same data type (also called mode). R supports many dierent mode: integer, double, logical, character and complex.

  • Vector con>nued- crea>ng a vector

    To create a vector use the func>on vector, or simply create a new variable. x1 on func>on c or :. X3

  • Vector con>nued

    We use [] to access the elements of a vector. Thus x[1] is the rst element of x, etc... x3[1]+2 x3[3:10] X3[1] Note: the index in R is 1 based!

  • Vector con>nued

    x1

  • Vector con>nued

    Again: in a vector all elements are of the same data type.

    If need be R will coerce your input in order to enforce that rule x1

  • Vector con>nued

    You can name the elements of a vector. x=1:5 names(x)=c(a,b,c,d,e) #we can do it more quickly: names(x)=lepers[1:5]

  • Vector con>nued

    Calculus is vectorized in R. A func>on that can operate on a data type will operate on a vector of such data type x=c(1,3) exp(x) x/2

  • Vector con>nued The recycling rule: consider the following example.

    xon func>on v+1 x+1

    In formal calculus, the last two expressions are not valid because the vectors are not of the same size. That is where the recycling rule kicks in: R repeats the shortest vector un>l the two vector have the same length.

  • Vector con>nued

    Accessing elements of collec>ons of objects is an important opera>on(subleqng).

    R provides a very powerful and exible facility for this.

    We can select subsets of a vector by inclusion, exclusion, by name and by logical indexing. The result is another vector.

  • Vector con>nued

    Can you guess what is going on? x=rbinom(10,21,0.5)

    by inclusion x[1]

    by exclusion x[-c(1,2,9,10)]

    by logical indexing x[x>5]

  • Vector con>nued

    Can you guess what is going on? x=1:10; names(x)=lepers[1:10] x[b] x[-(1:2)]=10:3 x[x==100]=NA x[c(TRUE,FALSE)] x[]=10 x=10

  • Logical vectors

    Most of you are familiar with vectors of numerics. It is important to understand vectors of logical.

    Logical can be either TRUE, FALSE or NA (for missing).

    These variables obey the rules of Boolean algebra with operators AND, OR and NOT.

    In R,AND is &,OR is |and NOT is !.

  • Logical vectors con>nued

    A=c(TRUE,TRUE); B=c(TRUE,FALSE) !A A&B A|B

  • Logical vectors con>nued

    logical vectors arise typically as the result of rela>onal opera>ons (that is comparison operators). x=1:5 x>2 x

  • Logical vectors con>nued

    R allows to do usual calcula>ons on logical vectors. In this case, the value TRUE is taken as 1 and FALSE as 0. This can be very useful in prac>ce. Consider the following: x=rnorm(100) mean(x>0)

  • Matrix

    R supports matrices and has a good numerical linear algebra library.

    You can create matrix in R using the func>on matrix.

    By default the matrix is lled by column. Use the argument byrow to ll the matrix by row. M1=matrix(data=1:8,ncol=4, nrow=2) M2=matrix(data=1:8,ncol=4,nrow=2,byrow=T)

  • Matrix con>nued

    You can name to rows and columns of a matrix: rownames(M1)=lepers[1:2] colnames(M1)=lepers[1:4]

    We can nd out the size of a matrix: dim(M1) ncol(M1) nrow(M1)

  • Matrix con>nued

    To index a matrix, use the same techniques as for vectors but with the rst index for rows and the second index for column. Can you guess what is is going on? M1= matrix(data=1:8,ncol=4, nrow=2) rownames(M1)=lepers[1:2] colnames(M1)=lepers[1:4] M1[-1,2] M1[a,] M1[,c(TRUE,TRUE,FALSE)]

  • Matrix con>nued

    When we take a one-dimensional subset of a matrix, the result by default is coerced into a vector, unless we use the argument drop. M1[,1] # compare with M1[,1,drop=F]

  • Matrix con>nued

    R supports matrix calculus, but beware: A+B, A-B, A/B, A*B are all element by element opera>ons. As with vectors, any opera>on on numerics will also work on matrix of numerics. A=matrix(1:4,2,2) B=matrix(5:8,2,2) A+B A/B A*B log(exp(A))

  • Matrix con>nued

    A% %B is the usual matrix mul>plica>on. A% %B is the usual matrix mul>plica>on. The inverse is obtained with the func>on solve.

    Other useful func>on: eigen, det. The transpose is t.

  • Matrix con>nued

    C = diag(2) eig = eigen(A) det(A-eig$values[1]*C); det(A-eig$values[2]*C) B=solve(A) #return the inverse of A

  • Matrix con>nued Consider the matrix

    Write some R code to do the following. 1. add row3 to row1. 2. interchange column 1 and column 4 3. Calculate AA, ( A means the transpose of A). Is A matrix symmetric ?

    A =4 1 2 31 3 1 22 1 2 13 2 1 1

  • Matrix con>nued

    A=matrix(c(4,1,2,3,1,3,1,2,2,1,2,1,3,2,1,1),4,4) A[1,]=A[3,]+A[1,] Z=A[,1] A[,1]=A[,4] A[,4]=Z AA=A%*%t(A) all(A==t(A))

  • Next Lecture

    Factor Dataframe List