Professional Documents
Culture Documents
Intro To R Lecture 2
Intro To R Lecture 2
Intro To R Lecture 2
195
Lecture
2
Xiaotong
Suo
xiaotong@stanford.edu
More logis>cs
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
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(double,length=20)
#
a
vector
of
real
numbers
x2
<-
c(1:10)
#
a
vector
of
integers
x3
<-
c(T,F,FALSE,TRUE)
#
a
vector
of
logical
values
x4
<-
c(MY,NAME,
IS)
#
a
vector
of
characters
X5
<-
c(2+2i,complex(real=cos(pi/3),
imaginary=sin(pi/3)))
typeof(x4)
#or
class(x4)
or
mode(x4)
typeof(x5)
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
<-
c(1.2,a)
x2
<-
c(5,-3,FALSE)
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.
x<-c(1,2,3,5);
y
=
1:5
z=seq(from=1,to=2,by=0.5)
length(z)
#will
give
you
the
number
of
elements
of
z
v=c(x,y,z)
#
c
is
the
concatena>on
func>on
v+1
x+1
Vector
con>nued
A
ccessing
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
M
ost
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
!.
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]
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
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
4 1 2 3
1 3 1 2
A=
2 1 2 1
3 2 1 1
Write
some
R
code
to
do
the
following.
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