Training on R For 3 rd and 4 th Year Honours Students, Dept. of Statistics, RU Empowered by Higher Education Quality Enhancement Project (HEQEP) Department of Statistics Rajshahi University, Rajshahi-6205, Bangladesh March 21-23, 2013 Installation and Data Structures of R
41
Embed
Training on R For 3 rd and 4 th Year Honours Students, Dept. of Statistics, RU
Training on R For 3 rd and 4 th Year Honours Students, Dept. of Statistics, RU. Installation and Data Structures of R. Empowered by. H igher E ducation Q uality E nhancement P roject (HEQEP) Department of Statistics Rajshahi University, Rajshahi-6205, Bangladesh - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Training on RFor 3rd and 4th Year Honours Students, Dept. of Statistics, RU
Empowered by
Higher Education Quality Enhancement Project (HEQEP)Department of Statistics
Rajshahi University, Rajshahi-6205, Bangladesh
March 21-23, 2013
Installation and Data Structures of R
Statistical Programming Language S developed at Bell Labs, 1976.
Licensed as S-Plus in 1983.
1990 : R An open source program similar to S
Developed by Robert Gentleman and Ross Ihaka (Auckland, NZ)
# Generate a 3 by 4 array> x <- 1:12> dim(x) <- c(3,4)> x [,1] [,2] [,3] [,4][1,] 1 4 7 10[2,] 2 5 8 11[3,] 3 6 9 12
The dim assignment function sets or changes the dimension attribute of x, causing R to treat the vector of 12 numbers as a 3 × 4 matrix.
Notice that the storage is column-major; that is, the elements of the first column are followed by those of the second, etc.
# Generate a 4 by 5 array> A <- array(1:20, dim = c(4,5)) > A [,1] [,2] [,3] [,4] [,5][1,] 1 5 9 13 17[2,] 2 6 10 14 18[3,] 3 7 11 15 19[4,] 4 8 12 16 20
Array & MatrixA matrix in mathematics is just a two-dimensional array of numbers. Matrices and arrays are represented as vectors with dimensions:
Array & MatrixA matrix in mathematics is just a two-dimensional array of numbers. Matrices and arrays are represented as vectors with dimensions:
# 3 x 2 matrix of 0> Y <- matrix(0, nrow=3, ncol=2) > Y [,1] [,2][1,] 0 0[2,] 0 0[3,] 0 0
# Generate a 3 by 2 Matrix > A = matrix(1:12, nrow=3, byrow=T)> A [,1] [,2] [,3] [,4][1,] 1 2 3 4[2,] 5 6 7 8[3,] 9 10 11 12
> A[ ,2] # 2nd column of matrix A[1] 2 6 10
> A[3, ] # 3rd row of matrix A[1] 9 10 11 12
> A[2 ,2] # (2, 2) th element of matrix A[1] 2 6 10
Basic operations – MatrixR command Purpose (output)A+B addition of A and B matricesA * B element by element productsA %*% B product of A and B matrices t(A) transpose of matrix Asolve(A) inverse of matrix Acbind() forms matrices by binding together
matrices horizontally, or column-wise
rbind() forms matrices by binding together matrices vertically, or row-wise
vector: an ordered collection of data of the same type. > a = c(7,5,1)> a[2][1] 5
list: an ordered collection of data of arbitrary types. > a = list(Name="Rahim",age=c(12, 23,10), Married = F)> a$Name[1] "Rahim"$age[1] 12 23 10$Married[1] FALSE
Typically, vector elements are accessed by their index (an integer), list elements by their name (a character string).
List
Data frames Data frame is supposed to represent the typical data table that
researchers come up with – like a spreadsheet. It is a rectangular table with rows and columns with same length; data
within each column has the same type (e.g. number, text, logical), but different columns may have different types.