Upload
rory
View
29
Download
0
Tags:
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
Introduction
Session 1
Elements of a storyvs.
Elements of a Program
• 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
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
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
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
Vectors
fruitbasket1 2 3 4
5 6 7
Named Vectors
fruitbasketapple pineapple apple2 banana banana2 orangebanana3
Matrix
Array
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
Lists
Fruits
$Californiaapples peaches oranges bananas
$Floridaoranges apples lemons
$Hawaiicoconuts pineapplesoranges papaya durian
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
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)
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
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
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)
}