1 / 17

Introduction

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.

rory
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction Session 1

  2. Elements of a storyvs.Elements of a Program

  3. 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

  4. 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

  5. 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

  6. 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

  7. Vectors fruitbasket 1 2 3 4 5 6 7

  8. Named Vectors fruitbasket apple pineapple apple2 banana banana2 orange banana3

  9. Matrix

  10. Array

  11. Data Frames fruitbasket

  12. Lists Fruits $California apples peaches oranges bananas $Florida oranges apples lemons $Hawaii coconuts pineapples oranges papaya durian

  13. 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

  14. 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)

  15. 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

  16. 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

  17. 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) }

More Related