170 likes | 272 Views
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.
E N D
Introduction Session 1
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
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 fruitbasket 1 2 3 4 5 6 7
Named Vectors fruitbasket apple pineapple apple2 banana banana2 orange banana3
Data Frames fruitbasket
Lists Fruits $California apples peaches oranges bananas $Florida oranges apples lemons $Hawaii coconuts pineapples oranges 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 Carl Bob Dee
Christmas Shopping Human 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 #s Human 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] = 0 myFibs[2] = 1 for(spot in 3:n){ myFibs[spot] = myFibs[spot-1] + myFibs[spot-2] } return(myFibs) }