Unformatted text preview:

operatorscomparisonassignmentlogical arithmeticvariablesregular variables (a.k.a. scalars) vectorsmatrices and arraysdata framesfunctions control flowif()for()while()data creationinput and outputc()read.table()read.delim()write.table()the clipboard (in Windows only)Mac userswriting programseditorsgeneric editorsjEditWindows userscommentingKISS codehelp!Summary of R learned in this chapteroperatorslogical arithmeticvariablesgeneral functions control flow functionsdata import and export394U – DIY Stats – Cormack - Programming primer operators comparison Comparison operators compare two values (hence the name…). Importantly, they do not destroy or overwrite anything, unless combined with an assignment operator. The comparison operators are: <, >, <=, >=, = =, and != These should be fairly obvious – less than? greater than? less than or equal to? etc. Note that the double equals sign is not a typo – “is equal to?” is a double equals sign; this is what distinguishes it from the assignment operator. ( ! means “not” in most programming languages, so the last one is “not equal to?”) assignment = (the equals sign) assigns a value (on the right) to a variable (on the left). Importantly, if the variable already exists, it will be overwritten with the new value. Seriously, if “x” contains all the data that the Air Force uses to drop bombs, and you type x = 4 then too bad for the Air Force – x now contains the value 4 – no recovery, no exceptions, all bombs go to “4” (wherever that is). One seeming exception to the “= destroys things” rule – and a very useful one – is as follows. Programming languages, unlike English, are read right-to-left by default. Thus x = x + 1 will take the existing value of x, add one to it, and store the result back into x. The old value of x is gone forever, but it was used in an intermediate step to calculate the new value of x. (Hard-core R snobs insist on using <- for assignment. I don’t care, and you probably shouldn’t either.) logical Logical operators combine logical values using AND, OR, NOT, and exclusive OR (XOR) (if you’ve taken logic out of a philosophy dept., congratulations! – it was not a completely wasted semester). They are very useful when we need to determine if a value is within or without a particular interval (as we do in hypothesis testing, say). The operators are: ! (NOT), & (AND), | (OR), and xor(value1, value2) (XOR).Here is an example. x = rnorm(100) This puts 100 normally distributed random numbers (with mean 0 and standard deviation 1) in the vector x. y = (x < -2) | (x > 2) This finds each case in which the value is more than 2 standard deviations from the mean. In English, it reads “Set y to TRUE for each value of x that is less than –2 OR greater than 2. The parenthesis ensure that the operations are done in the right order (evaluate the less than and the greater than, then evaluate the OR), just like in high school algebra. arithmetic +, -, *, %, and ^ should be obvious. %/% gives the integer portion of division. %% gives the remainder of division. The : (colon) isn’t an operator in the strict sense, but I’ll introduce it now anyway. It stands for “through” in that 1:5 generates the numbers 1 through 5. More usefully, x = 1:5 generates the integers 1 through 5 and stores them in the vector x. I include it (the : ) here because you can think of it as an operator that keeps adding (a true operation) 1 to the first number until it gets to the second number (sue me if you don’t like it). variables regular variables (a.k.a. scalars) A variable can be thought of as a container, referred to by the variable name, that can hold any numeric value. For example, after x = 5 the variable x contains the value 5. I think of it like a mailbox. I don’t have to go around to various places to collect my daily mail. Instead “today’s mail” (the value) appears in the mailbox “Cormack” (the variable name). So all I have to do each day is query the variable “Cormack”, which makes checking my mail easy.You will see the tremendous value in this when we start writing longer programs, especially those with loops. vectors A vector consists of multiple values, all referred to by the same variable name. The individual values are referred to by the variable name and an integer index. For example, after x = c(1, 2, 3, 5, 7) (see “data creation” below), x contains the first five prime numbers. Elements are accessed using square brackets, so the 4th value is obtained by x[4], etc. The number inside the brackets is the index into the vector x. You can also refer to ranges of values – so x[2:4] are the middle three values, and, as I mentioned above, “2:4” is R for “2 through 4”. You can think of a vector as a row of mailboxes, like we have on rural Texas roads (where all the mailboxes for the houses down a particular turn-off are in a single row at the turn-off). So if there is a row of 5 mailboxes at the turnoff to Guntotin Rd., then we could name the entire row “guntotin” and guntotin[3] would then refer to the middle mailbox. In addition to the above use of c(), there are two other ways to create vectors. The first is to use the numeric() function: x = numeric(5) which creates a vector 5 elements long and fills it with zeroes. The second way to create a vector is implicit. If you create a scaler variable – a variable containing a single value – and then try to set a value at an index larger than 1, then R will automatically expand the scaler into a vector of the appropriate length. For example: x = 5 x[4] = 13 In the second line, x is originally a scaler, but since we tried to set the 4th element equal to something, R automatically expanded x into a 4-element vector, and set the 4th element to 13. matrices and arrays Matrices are the same as vectors except they have both rows and columns, i.e. they are 2 dimensional instead of 1 dimensional. Matrices are created using the matrix() function: y = matrix(0, 2, 5) The variable y is now a matrix with 2 rows and 5 columns, filled with the value 0. Individual values in a matrix are set or obtained just as with vectors, except both a row index and a column index are needed. Thusy[2, 3] = 2.718282 Sets the element at the second row and third column to e (roughly), and my.e = y[2, 3] grabs this value and places it in my.e. You can think of matrix as being like the


View Full Document

UT PSY 394U - Programming primer

Documents in this Course
Roadmap

Roadmap

6 pages

Load more
Download Programming primer
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 Programming primer 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 Programming primer 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?