Unformatted text preview:

What is a computation?Transformation from one or more inputs to an output.Transition from old state to new stateAlgorithm set of directions for carrying out a computation in terms of other simplercomputations.Examples• Find the average annual rainfall at a weather station• Crop a digital photo• Sort the mail by sender in your mail program– Typeset by FoilTEX – 1R• Interactive language for expressing statistically-oriented computations– Mathematics– Data cleaning and processing– Exploratory data analysis– Statistical methodology– Simulation– Graphics• Language for creating new functionality– Typeset by FoilTEX – 2Operations• Invoke a computation with an expression• Pass the expression off to the computer to evaluate• Return a value or output of the expressionIdentify the inputs and output of the following expression.2 + 32Infix: In R we would write 2 + 3ˆ 2, where ˆ represents exponentiation.The+ and ˆ are infix computations.– Typeset by FoilTEX – 3Function Style Computations• R uses both infix style computations and function style computations.• The format for calling a function to perform a computation:functionName(argument, ..., argument)• For example,rnorm(10)generates 10 random values from a standard normal distribution(i.e. Normal with mean 0 and standard deviation 1).• Arguments can also be identified by namernorm(10, sd = 5)generates 10 random values from a normal distribution with mean 0and standard deviation 5.– Typeset by FoilTEX – 4Examples• Single Expression:rnorm(10, sd = 5)• Compound Expression:mean( rnorm(10, sd = 5) )• Ill-formed Expression:rnorm(10; sd = 5)Error: syntax error– Typeset by FoilTEX – 5ParsingBreak down an expression into parts, which we call tokens.How to separate out the pieces?• White Space: 22 2 vs 222• Atomic Tokens:22+2 same as 22 + 2 but not the same as 2 2 + 2• Digits: 2x vs 2*x• Naming Conventions: x22 vs 22x• Quotation Marks:“Hi” and ’Bye’ are valid character values, but “My’ is not• New Line:What does the computer return for each of these expressions?2 + 342+34(2 + 34)– Typeset by FoilTEX – 6To Do• Write the following expression in infix notationpower(divide(10, divide(15, 3)), minus(12, power(2, 3)))• Evaluate the following expression using the traditional order of operations.3 − 5 +46− 2 × 32• Circle the tokens in the following expression. How many are there?cat = (1 + x11)ˆ24•What is the difference between e1, 1e, and 1e1?– Typeset by FoilTEX – 7Output and Assignment• When we evaluate a command, R prints the results to the screen as output.• How do we get to use it again, e.g. as input to other commands?x = rnorm(10)• The above assignment statement puts the result from the command rnorm(10) into avariable named x.• We can see the value of x via the simple expressionx = rnorm(10)• This is the sames as“Evaluate x and print the result”i.e. provide the current ocntents of the variable x.• In R we can also use the left arrow to assign a value to a variable:x <− rnorm(10)– Typeset by FoilTEX – 8VariablesVariables have a name and a value.To access the value we use the name. Variables allow us to• Store state on the computer• Store a value without needing to recompute it• Write a general expression, e.g. sqrt(aˆ 2 + bˆ 2)• Reduce redundancy (and mistakes)n = 10x = rnorm(n)sum(x) / n– Typeset by FoilTEX – 9Variable NamesVariable Names must follow some rules:• May not start with a digit or underscore• May contain numbers, characters (upper and lower case), and some punctuation,period . and underscoreare okay,but most other other are not, e.g. commas, quotation marks, and # are not.• Case-sensitive, so x and X are different.• Use meaningful names.• Avoid names that have a meaning in R, e.g. function names such as c, t, s, .C– Typeset by FoilTEX – 10Managing VariablesWe can manage our variables with R functions• List all variablesobjects()• Remove one or more variablesrm(x, y)• Save variables for future usesave(x, y, z, file = “myfile.rda”)• Restore variablesload(“myfile.rda”)• Alternatively, an entire workspace may also be saved, and it will be automatically loadedwhen you start R up again.¿ q()Save workspace image? [y/n/c]:– Typeset by FoilTEX –


View Full Document
Download What Is A Computation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view What Is A Computation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view What Is A Computation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?