DOC PREVIEW
UT PSY 394U - Programming Primer

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

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!394U – 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” (thevariable 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. The 4th value is obtained by x[4]. 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. 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. Individual values in a matrix are obtained by using a row index and a column index. Thus y[2, 5] will refer to the value in the second row and third column of the matrix y. You can think of matrix as being like the faculty or student mailboxes in the mailroom. If the name “faculty” referred to the faculty mailboxes, then faculty[4, 5] would be my mailbox, since it is fourth from the top and fifth from the left. You can also refer to entire rows – faculty[4, ] would be the fourth row, all columns of faculty – or entire columns – faculty[ , 5] is the fifth column, all rows of faculty. As with vectors, you can obtain subsets –faculty[1:3, c(5,7,9)] would be the top three mailboxes in columns 5, 7, and 9. Matrices have 2 dimensions by definition, and are a special case of arrays, which can have any number of dimensions. If you are new to programming, and you try to work with arrays with more than 3 dimensions, your brain will probably explode. data frames Data frames are a convenient type of matrix. We’ll cover data frames later. functions Functions are fancy, black-box versions of operators. They take zero or more inputs, called arguments, and produce one or more outputs, called return values. In R, sum() is a function version of +, so sum(2,2) will


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?