Class Notes
(806,888)

Canada
(492,503)

University of Waterloo
(18,164)

Statistics
(474)

STAT 331
(24)

Kun Liang
(6)

Lecture

# Stat331 R-Tutorial1.pdf

Unlock Document

University of Waterloo

Statistics

STAT 331

Kun Liang

Fall

Description

STAT 331 - Tutorial 1
1 Some R Basics
1.1 Vectors and assignment
▯ Use the function c( ) to set up a numeric vector named y, say, consisting of ﬁve numbers,
namely 10:4, 5:6, 3:1, 6:4 and 21:7,
> y assign("y", c(10.4, 5.6, 3.1, 6.4, 21.7)
1.2 Vector arithmetic
▯ Vectors can be used in arithmetic expressions, in which case the operations are performed
element by element.
▯ The elementary arithmetic operators are the usual: +, ▯, ▯, = and ^ for raising to a power.
> 1/y
[1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
▯ The common arithmetic functions are available:
log( ), exp( ), sqrt( ), max( ), min( ), sum(), prod( ), mean( ), var( ), etc.
For example, sample variance of y can be calculated by
> sum((y-mean(y))^2)/(length(y)-1)
1.3 Logical vectors
▯ The logical operators are , >=, == for exact equality and != for inequality.
▯ In addition if c1 and c2 are logical expressions, then c1 & c2 is their intersection ("and"), c1
| c2 is their union ("or"), and !c1 is the negation of c1.
▯ The elements of a logical vector can have the values TRUE, FALSE.
1 > temp 10
> temp
[1] TRUE FALSE FALSE FALSE TRUE
1.4 Arrays and matrices
▯ The function array(data_vector, dim_vector) can be used to generate matrices.
> x x
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
▯ Equivalently we can use
> x x %▯% t(x)
is the matrix product.
> x ▯ x
is the matrix of element by element products
▯ Forming matrices using function cbind( ) and rbind( ). Roughly cbind( ) forms matrices by
binding together matrices horizontally, or column-wise, and rbind( ) vertically, or row-wise.
> cbind(y,x)
[1,] 10.4 1 6
[2,] 5.6 2 7
[3,] 3.1 3 8
[4,] 6.4 4 9
[5,] 21.7 5 10
1.5 Reading data from ﬁles
▯ Use the function read.table( )
The read.table( ) function will let you read in any type of delimited ASCII ﬁle (text ﬁle) that
contain rectangular data. It can read in both numeric and character values. This is by far
the easiest and most reliable method of entering data into R. To read an entire data frame
directly, the external ﬁle will normally have a special form.
2 – The ﬁrst line of the ﬁle should have a name for each variable in the data ﬁle.
– Each additional line of the ﬁle has the values for each variable.
For example, the ﬁrst few lines of a ﬁle to be read as a data frame might look as:
Price Floor Area Room Age Cent.heat
52.00 111.0 830 5 6.2 no
54.75 128.0 710 5 7.5 no
57.5 101.0 1000 5 4.2 no
...
The function read.table( ) can then be used to read the data frame directly.
> HousePrice data( )
▯ Editing data
When invoked on a data frame or matrix, edit brings up a separate spreadsheet-like envi-
ronment for editing. This is useful for making small changes once a data set has been read.
The command
> HousePriceNew ﬁtted.model fm

More
Less
Related notes for STAT 331