370 likes | 472 Views
Introduction. Session 1. R as a Giant Calculator. Demo: Set 1. Now try it yourself. log(), exp (), log10(), sqrt (), factorial(), choose(), floor(), ceiling(), trunc (), round(), signif (), cos (), sin(), tan(), acos (), asin (), atan (), acosh (), asinh (), atanh ()
E N D
Introduction Session 1
Now try it yourself log(), exp(), log10(), sqrt(), factorial(), choose(), floor(), ceiling(), trunc(), round(), signif(), cos(), sin(), tan(), acos(), asin(), atan(), acosh(), asinh(), atanh() Use help() to figure out what these functions do, and play with them
According to Wikipedia, Computer programming (often shortened to programming, scripting, or coding) is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages (such as Java, C++, C#, Python, etc.). The purpose of programming is to create a set of instructions that computers use to perform specific operations or to exhibit desired behaviors. The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic. Within software engineering, programming (the implementation) is regarded as one phase in a software development process.
According to Wikipedia, Computer programming (often shortened to programming, scripting, or coding) is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages (such as Java, C++, C#, Python, etc.). The purpose of programming is to create a set of instructions that computers use to perform specific operations or to exhibit desired behaviors. The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic. Within software engineering, programming (the implementation) is regarded as one phase in a software development process.
But we’re not software engineers Computer programming (often shortened to programming, scripting, or coding) is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages (such as Java, C++, C#, Python, etc.). The purpose of programming is to create a set of instructions that computers use to perform specific operations or to exhibit desired behaviors. The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic. Within software engineering, programming (the implementation) is regarded as one phase in a software development process.
In other words, • A program is plan of action, or a story • And programming is the creative process of making the plan or writing the story
Hypothetical Program/Passage You don’t need to be able to understand all of the following. Just read it for now and think about it.
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)
R • R is programming language designed for statistical computing, graphics, and data analysis • It’s open source, meaning that the source code is also is freely accessible. So the benefit is that it’s free, and easily updated with new methods and techniques
Demo 1: Introduction • This will be a gentle ease in • It won’t be extremely fun, but I believe that once you stick this out, the rest will be a lot more understandable and interesting
Demo 2: Data and Graphics Google Flu Trends Data Sources: http://www.google.org/flutrends/us/data.txt http://www.cdc.gov/flu/weekly/ussurvdata.htm
Demo 3: Working With Packages and a Bit of Math Radiohead Data Source: http://code.google.com/p/radiohead/
Demo 3: String Processing, Web Scraping, and Large Datasets State of the Union Addresses Data Source: http://www.presidency.ucsb.edu/sou.php
Demo 4: Mark Up Languages Flu Activity Using XML
Demo 4: Mark Up Languages Or Alternatively: Make a Website Using HTML
Other Demos (to be planned…) • Unix and Remote Computing May use Amazon Cloud for this Demo, but I know a few of you also have Biowulf accounts, so that would be easier • Python Tutorial This will also be a bit dry, but the exciting part is that you should now be able to learn a new language very quickly • Introductory Sequence Processing If you’re interested in genomics, this will be fun, and even if you’re not, I think this can also be fun. • Search Algorithms This is more theoretical, but it still is useful. I’ve had to whip this out of my toolkit a couple times this year. I think it’s really fun, though you can tell me if you think I’m lying. And we can demo this through searching mazes. • Suggestions?
Variables, Primitive Data Types, Data Structures, and Functions
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
Logical Data • Also called “boolean” data • In R, it is represented as TRUE/FALSE • But it can also be represented as 1/0
OR • Alice is trying to pick out a gift for Bob • Bob wants either something red OR a pen • Will he be happy with • A red pen? • A red candle? • A blue pen? • A blue candle?
OR Gift Is it red? Is it a pen? Happy? red pen TRUE TRUE TRUE red candle TRUE FALSE TRUE blue pen FALSE TRUE TRUE blue candle FALSE FALSE FALSE
AND • Alice is trying to pick out a gift for Bob • Bob wants either something red AND a pen • Will he be happy with • A red pen? • A red candle? • A blue pen? • A blue candle?
AND Gift Is it red? Is it a pen? Happy? red pen TRUE TRUE TRUE red candle TRUE FALSE FALSE blue pen FALSE TRUE FALSE blue candle FALSE FALSE FALSE
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) }