Intro To R Lecture 3
Intro To R Lecture 3
3
Xiaotong
Suo
Logis1cs
HW
1
due
on
Wednesday
before
the
class.
No
late
homework
will
be
accepted.
Note
that
you
have
to
get
at
least
60%
on
each
homework
to
pass
this
class.
Todays agenda
Matrix
Factor
List
Dataframe
Matrix
con1ued
The
func1on
order
returns
the
ascending
(default)
or
descending
order
of
elements
of
a
vector.
For
example,
order(0.5,5,-3.1)
will
return
3,
1,
2.
Its
sister
func1on
is
sort.
We
always
have
x[order(x)]=sort(x).
The
matrix
stack.x
is
a
readily
available
matrix
in
R.
It
has
three
names
columns:
Air.Flow,
Water.Temp,
Acid.Conc..
The
meaning
of
these
numbers
are
not
very
important
here.
Order
the
rows
of
the
matrix
by
increasing
values
of
the
column
AirFlow.
Factor
In
R
a
factor
is
a
vector
that
hold
categorical
data.
(for
example,
Yes-No
answers).
f1=factor(Yes,levels=c(Yes,No))
f1[2]=No
f1[3]=Maybe
f1[3]=No
f1[4]=No
table(f1)
Factor
Some1mes
we
want
to
keep
an
ordering
between
the
levels
of
the
factors
data=sample(le]ers[1:10],size=50,replace=T)
f2=factor(data,levels=le]ers[1:10],ordered=T)
Factor
con1nued
Some1mes
it
is
useful
to
convert
a
numerical
vector
into
factor.
The
func1on
cut
can
be
useful
in
doing
so.
data=stack.x[,Water.Temp]
f3=cut(data,breaks=3,labels=c(Low,Medium,Hi
gh))
#we
can
add
the
ordering
f3=cut(data,breaks=3,
labels=c(Low,Medium,High),ordered=T)
List
Lists
are
collec1ons,
like
vectors.
But
unlike
vectors,
the
same
list
can
hold
dierent
types
of
objects.
Lists
also
have
the
a]ribute
names
that
you
can
set
and
use
to
access
the
components.
You
can
also
access
the
components
using
the
operator
[[
]]
not
[
]
as
for
vectors
and
matrices.
Why
use
lists:
it
allows
you
to
put
together
objects
of
dierent
types.
Vectors
dont.
List
con1nued
V=c(A,A,C,B,B);
X=1:5;
Y=rnorm(5);
L=list(X,Y,V)
names(L)
names(L)=c(V1,V2,V3)
L$V1
L[[3]]
Data
frame
Roughly
a
data
frame
is
a
rectangular
table
with
rows
and
columns.
The
columns
represent
variables
and
can
be
of
dierent
types
(numeric,
integer,
factor,
character);
a
major
dierence
from
matrices.
The
rows
are
records
or
cases.
Typically
we
create
data
frames
by
reading
data
from
les.
We
can
also
create
data
frames
from
vectors
with
the
func1on
data.frame.
You
can
convert
a
data
frame
to
a
matrix
with
data.matrix.
dt2=airquality
dim(dt2);
names(dt2)
edit(dt2)
#Compare
and
understand
the
following
mean(dt2$Ozone);
mean(dt2$Ozone,na.rm=T)
#Be]er
yet:complete.cases
Ind=complete.cases(dt2)
dt=dt2[Ind,]
mean(dt$Ozone)
#or
mean(dt[,1])
or
mean(dt[[1]])
plot(dt$Ozone,type=l)
Next
Lecture
Data
Input/output
Graphics
More
func1ons