+ All Categories
Home > Documents > Objects in R -...

Objects in R -...

Date post: 05-Jun-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
22
Object What is an Object? Object Types Operations Your Data Objects in R Andrew Robinson Department of Mathematics & Statistics University of Melbourne February 15, 2006 Andrew Robinson Objects in R
Transcript
Page 1: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Objects in R

Andrew Robinson

Department of Mathematics & StatisticsUniversity of Melbourne

February 15, 2006

Andrew Robinson Objects in R

Page 2: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Outline

1 Object

2 What is an Object?Why use Objects?Why avoid Objects?

3 Object Types

4 OperationsSize-preservingSize-increasingSize-reducingSize-altering

5 Your Data

Andrew Robinson Objects in R

Page 3: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Why use Objects?Why avoid Objects?

Characteristics

Everything.

Objects are realizations of a class.

All objects have attributes.

Andrew Robinson Objects in R

Page 4: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Why use Objects?Why avoid Objects?

Characteristics

Everything.

Objects are realizations of a class.

All objects have attributes.

Andrew Robinson Objects in R

Page 5: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Why use Objects?Why avoid Objects?

Characteristics

Everything.

Objects are realizations of a class.

All objects have attributes.

Andrew Robinson Objects in R

Page 6: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Why use Objects?Why avoid Objects?

Simplicity

Using objects simplifies many complicated problems.

1 Communication.

2 Comparison.

3 Coercion.

Andrew Robinson Objects in R

Page 7: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Why use Objects?Why avoid Objects?

Memory

You have to put them somewhere.

Andrew Robinson Objects in R

Page 8: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Basic

Numeric.

String.

Factor.

Integer.

Logical.

Andrew Robinson Objects in R

Page 9: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Containers

Vector.

Matrix.

Array.

Dataframe.

List.

Andrew Robinson Objects in R

Page 10: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Creation and Assignment

<-

Examples

my.new.object <- c(1:10)

my.dear.friends <- c("Val", "Peter")

Andrew Robinson Objects in R

Page 11: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Creation and Assignment

<-

Examples

my.new.object <- c(1:10)

my.dear.friends <- c("Val", "Peter")

Andrew Robinson Objects in R

Page 12: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Arithmetic

Examples

num.1 <- 1

num.2 <- 2)

num.3 <- num.1 / num.2

Arithmetic is vectorized. This is an example of coercion.

Examples

num.1 <- c(1:10)

num.2 <- c(1:10)

num.3 <- num.1 / num.2

Andrew Robinson Objects in R

Page 13: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Arithmetic

Examples

num.1 <- 1

num.2 <- 2)

num.3 <- num.1 / num.2

Arithmetic is vectorized. This is an example of coercion.

Examples

num.1 <- c(1:10)

num.2 <- c(1:10)

num.3 <- num.1 / num.2

Andrew Robinson Objects in R

Page 14: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Combinations

Objects can be combined or concatenated in various ways.

Relevant commands

cbind()

rbind()

merge()

Andrew Robinson Objects in R

Page 15: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Combinations

Objects can be combined or concatenated in various ways.

Relevant commands

cbind()

rbind()

merge()

Andrew Robinson Objects in R

Page 16: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Combinations

Objects can be combined or concatenated in various ways.

Relevant commands

cbind()

rbind()

merge()

Andrew Robinson Objects in R

Page 17: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Subsetting

[ ]

Examples

my.object <- c("A", "B", "C")

my.object[2]

my.object[2:3]

my.object[-1]

Andrew Robinson Objects in R

Page 18: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Subsetting

[ ]

Examples

my.object <- c("A", "B", "C")

my.object[2]

my.object[2:3]

my.object[-1]

Andrew Robinson Objects in R

Page 19: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Reshaping objects

Sometimes objects are in the wrong shape.

Relevant Function

reshape()

Andrew Robinson Objects in R

Page 20: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Size-preservingSize-increasingSize-reducingSize-altering

Reshaping objects

Sometimes objects are in the wrong shape.

Relevant Function

reshape()

Andrew Robinson Objects in R

Page 21: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Your data are also an Object

R reads data from outside into objects, and writes objects to files .

Relevant Function

read.csv()

write.csv()

Andrew Robinson Objects in R

Page 22: Objects in R - web.forestry.ubc.caweb.forestry.ubc.ca/biometrics/documents/R-Workshop/objects.pdf · Object What is an Object? Object Types Operations Your Data Size-preserving Size-increasing

ObjectWhat is an Object?

Object TypesOperationsYour Data

Your data are also an Object

R reads data from outside into objects, and writes objects to files .

Relevant Function

read.csv()

write.csv()

Andrew Robinson Objects in R


Recommended