17
Introduction Session 1

Introduction

  • Upload
    rory

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction. Session 1. Elements of a story vs. Elements of a Program. Variables. Names, same as in life, for example, Alice, Bob, Carol, Fluffy They “store” data In this illustration, the variables are the name cups, not the actual people themselves. Alice. apple. dog. - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction

Introduction

Session 1

Page 2: Introduction

Elements of a storyvs.

Elements of a Program

Page 3: Introduction

• Names, same as in life, for example, Alice, Bob, Carol, Fluffy

• They “store” data

• In this illustration, the variables are the name cups, not the actual people themselves.

Variables

Aliceapple dog

Page 4: Introduction

Primitive Data Types

• Primitive data types serve as the fundamental building blocks for more complicated types of data.

• As a real-world analogy, I relate data to nouns, and primitive data to anything organic, occurring naturally like apples, dogs, people

Page 5: Introduction

Primitive Data Types in R

• R has three main primitive data types– Numeric (numbers, ex: 0, 5, 144, 25.7, Inf)– Character (words or passages, ex: “hello world”,

“apple”, “My fellow citizens, I stand here today humbled by the task before us”)

– Logical (TRUE/FALSE)• There is one exception, missing data is

represented as NA and has certain properties of its own

Page 6: Introduction

Data Structures

• Data structures are also data, but more complex- ways of storing and organizing the data so that it can be used efficiently.

• As a real world analogy, I think of data structures as containers, like chains, ice cube trays, apartment complexes, trains

Page 7: Introduction

Vectors

fruitbasket1 2 3 4

5 6 7

Page 8: Introduction

Named Vectors

fruitbasketapple pineapple apple2 banana banana2 orangebanana3

Page 9: Introduction

Matrix

Page 10: Introduction

Array

Page 11: Introduction

Data Frames

Fruit Weight StateApple 5 oz CaliforniaPineapple 16 oz FloridaApple 5.5 oz FloridaBanana 4 oz GeorgiaBanana 3.7 oz CaliforniaOrange 4.8 oz California

fruitbasket

Page 12: Introduction

Lists

Fruits

$Californiaapples peaches oranges bananas

$Floridaoranges apples lemons

$Hawaiicoconuts pineapplesoranges papaya durian

Page 13: Introduction

Christmas Shopping• A hypothetical program• Alice needs to buy Christmas

Presents for each of her cousins so

• She’s planning out her day• So her plan, which she calls

OperationXMas() should take in input a list of her 8 cousins

• And output a list of wrapped presents

Bob Carl Dee

Page 14: Introduction

Christmas ShoppingHuman language (what Alice is thinking in her mind):

My cousins are Bob, Carl, & Dee

Start out with no items bought

For each person, pick out a perfect present

Checking out all items in my shopping cart

Bring home what I bought

Programming Language (if Alice wanted to hire a robot to do her bidding):

Cousins = c(Bob, Carl, Dee)

cart = c()

for(each in Cousins){ cart = c(cart, PickPresent(each))}

cart = CheckOut(cart)

return(cart)

Page 15: Introduction

Generate the first N Fibonacci #s

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

The nth Fibonacci number is the sum of the previous two numbers. In other words,

Fn=Fn-1+Fn-2

F0=0, F1=1

Page 16: Introduction

Generate the first N Fibonacci #s

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

The nth Fibonacci number is the sum of the previous two numbers. In other words,

Fn=Fn-1+Fn-2

F0=0, F1=1

Page 17: Introduction

Generate the first N Fibonacci #sHuman language:

Generating a list of the first n Fibonacci numbers involves knowing what the value of “n” is.

Creating an empty vector called myFibs with n spots

Fill spot 1 with 0 and spot 2 with 1

For each spot number, starting with 3, and ending with n,

fill that spot with the sum of what’s in the previous two spots

Output the list of numbers

Programming Language:

fibonacci = function(n){

myFibs = rep(0,n)

myFibs[1] = 0myFibs[2] = 1

for(spot in 3:n){myFibs[spot] = myFibs[spot-1] + myFibs[spot-2]}return(myFibs)

}