rstudio vectors
play

RStudio, vectors Steve Bagley somgen223.stanford.edu 1 - PowerPoint PPT Presentation

RStudio, vectors Steve Bagley somgen223.stanford.edu 1 Introduction somgen223.stanford.edu 2 Course information and philosophy Course website: somgen223.stanford.edu Website has detailed syllabus, homework, links to videos


  1. RStudio, vectors Steve Bagley somgen223.stanford.edu 1

  2. Introduction somgen223.stanford.edu 2

  3. Course information and philosophy • Course website: somgen223.stanford.edu • Website has detailed syllabus, homework, links to videos • Structure of the course: • lecture with in-class interactive exercises • video/recording via Zoom. Videos should be available shortly after class ends. • Q/A using Zoom chat window during class • Q/A, announcements, discussion using Piazza outside of class • homework with rapid reinforcement (approx one per week) • review answers in class immediately after homework is due • no late submissions • drop lowest homework grade • final data analysis project somgen223.stanford.edu 3

  4. Using RStudio somgen223.stanford.edu 4

  5. The basics of interaction using the console window In RStudio the console window is the left (or lower-left) window. The R console uses a “read, eval, print” loop. This is sometimes called a REPL. • Read: R reads what you type … • Eval: R evaluates it … • Print: R prints the result … • Loop: (repeat forever) somgen223.stanford.edu 5

  6. > 1 + 2 [1] 3 A simple example in the console • This is an expression that will be evaluated by R, followed by the result of evaluating that expression. • 3 is the answer • [1] means: the answer is a vector and this line starts with the first element of that vector. • It does not mean the answer has one element, although that is true in this case. somgen223.stanford.edu 6

  7. The console • The console window is a great place to test something simple. • It is not a great place to develop and save your ideas. somgen223.stanford.edu 7

  8. R script pane • Use the menu item File / New File / R Script to create a new script pane. • You can save the contents of this pane to a file. • You can evaluate all or part of the script in the console window. • Hit Command-RETURN (Mac), or Ctrl-ENTER (Linux/Windows). • That line is automatically copied to the console pane and evaluated. somgen223.stanford.edu 8

  9. 1 + 2 1 + 2 1 + 2 1 + 2 Spaces (mostly) don’t matter • These all do the same thing. somgen223.stanford.edu 9

  10. 1 + 2 * 3 [1] 7 log (10) [1] 2.302585 [1] 3.141593 R is a scientific calculator 4 * atan (1) • * denotes multiplication. • log is the natural logarithm. • atan is the one-argument arctangent. • The result of each of these expressions is a vector of one element. somgen223.stanford.edu 10

  11. ## This is a comment 1 + 2 # add some numbers [1] 3 Adding comments • Use a # to start a comment. • A comment extends to the end of the line and is ignored by R. • The recipient of a comment is you in six months. somgen223.stanford.edu 11

  12. Vectors somgen223.stanford.edu 12

  13. 2 : 4 [1] 2 3 4 [1] 3 4 5 3 : 5 # same as above [1] 3 4 5 Creating vector of ascending numbers 1 + (2 : 4) • A vector is a one-dimensional sequence of zero or more numbers (or other values). • The colon : is an R operator to create a vector that is a sequence of integers from the first number to the second number (inclusive). • Many R functions and operators automatically work with multi-element vector arguments. somgen223.stanford.edu 13

  14. 8 [41] 40 41 42 43 44 45 46 47 48 49 50 [1] 0 1 2 3 4 5 6 7 0 : 50 9 10 11 12 13 14 15 16 17 18 19 [21] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Creating a longer vector • Long vectors wrap around. • Look at the [ ] notation. The second output line starts with 20, which is the 21 th element of the vector. • Your screen may have a different width than what is shown here. • This notation will help you figure out where you are in a long vector. • R starts counting at 1. somgen223.stanford.edu 14

  15. 1 8 [1] 0 : 10 2 3 4 5 6 7 9 10 11 9 10 11 (1 + 0) : 10 [1] 1 2 3 4 5 6 7 1 + (0 : 10) 8 9 10 7 [1] 0 1 2 3 4 5 6 8 7 9 10 1 + 0 : 10 # which operator gets executed first? [1] 1 2 3 4 5 6 8 Operator precedence examples • Note the subtle differences in these expressions and their results. • R does not evaluate strictly left to right. Instead, operators have their own precedence, a kind of priority for evaluation. • The sequence operator : has a higher precedence than addition + . • Use parentheses to enforce the desired evaluation order. somgen223.stanford.edu 15

  16. Assigning values to names somgen223.stanford.edu 16

  17. x <- 10 Saving values by setting a variable • To do more complex computations, we need to be able to give names to things. • Read this as “x gets 10” or “assign 10 to x” • R prints no result from this assignment, but what you entered causes a side effect: R has stored the association between x and 10. (Look at the Environment pane.) somgen223.stanford.edu 17

  18. x [1] 10 x / 5 [1] 2 Using the value of a variable • When R sees the name of a variable, it uses the stored value of that variable in the calculation. • Here R uses the value of x, which is 10. • / is the division operator. • We can break complex calculations into named parts. This is a simple, but very useful kind of abstraction. somgen223.stanford.edu 18

  19. x <- 10 x [1] 10 x = 20 x [1] 20 Two ways to assign In R, there are two assignment operators. They have subtly different meanings (more details later). • <- requires that you type two characters. Don’t put a space between < and - . (What would happen?) • RStudio hint: Use “ Option - ” (Mac) or “ Alt - ” (Linux/Windows) to type this using one key combination. • = is easier to type. • You will see both used throughout R and user code. somgen223.stanford.edu 19

  20. x <- 2232.348 x [1] 2232.348 ## later in your code: x <- 0 x [1] 0 Warning: Assignment has no undo • If you assign to a name with an existing value, that value is overwritten. • There is no way to undo an assignment, so be careful in reusing variable names. somgen223.stanford.edu 20

  21. Naming variables • It is important to pick meaningful variable names. • Names can be too short, so don’t use x and y everywhere. • Pick names that will make sense to someone else (including the person you will be in six months). • ADVANCED: See ?make.names for the complete rules on what can be a name. somgen223.stanford.edu 21

  22. a <- 1 A Error in eval (expr, envir, enclos) : object 'A' not found Case matters for names in R • Now a has a value. A does not have a value. • R cares about upper and lower case in names. somgen223.stanford.edu 22

  23. 81 100 49 16 9 4 1 [1] z z <- x ^ 2 49 64 64 36 36 25 16 9 4 1 [1] y y <- x * x x <- 1 : 10 81 100 25 Elementwise operations on a vector • This multiplies each element of x by the corresponding element of x , that is, it squares each element. • Equivalently, we could use exponentiation: somgen223.stanford.edu 23

  24. sqrt (2) [1] 1.414214 sqrt (0 : 10) [1] 0.000000 1.000000 1.414214 1.732051 2.000000 2.236068 [7] 2.449490 2.645751 2.828427 3.000000 3.162278 Calling built-in functions • To call a function, type the function name, then the argument(s) in parentheses. • Use a comma to separate the arguments, if more than one. • Nearly all R functions operate on multi-element vectors as easily as on vectors containing a single element. somgen223.stanford.edu 24

  25. x <- c (4, 0, 3) x [1] 4 0 3 Creating a vector from a list you specify • The c function (combine) makes a vector out of its arguments, which are separated by commas. • Very important: the input representation and output representation of a vector are not the same. • Input: c(4, 0, 3) • Output: [1] 4 0 3 • (Compare this to a Python list: [4, 0, 3] .) somgen223.stanford.edu 25

  26. weights <- c (1.1, 2.2, 3.3) Exercise (for you to do now): convert weights • These weights are in pounds. • Convert them to kilograms. • (Hint: 2.2 lb = 1.0 kg) somgen223.stanford.edu 26

  27. weights <- c (1.1, 2.2, 3.3) ## this divides the weights, element-wise, by the conversion ## factor: weights / 2.2 [1] 0.5 1.0 1.5 Answer: convert weights somgen223.stanford.edu 27

  28. sum (x) [1] 9.375 x [1] 9 12 6 10 10 16 8 4 length (x) [1] 8 x <- c (9, 12, 6, 10, 10, 16, 8, 4) [1] 75 sum (x) / length (x) [1] 9.375 mean (x) Some vector functions • The mean is the sum divided by the length. • There is a built-in mean function in R. somgen223.stanford.edu 28

  29. x <- c (7, 3, 1, 9) Exercise: subtract the mean • Subtract the mean of x from x , and then sum the result. somgen223.stanford.edu 29

  30. x <- c (7, 3, 1, 9) mean (x) [1] 5 x - mean (x) [1] 2 -2 -4 4 sum (x - mean (x)) # answer in one expression [1] 0 Answer: subtract the mean somgen223.stanford.edu 30

  31. m <- 13 se <- 0.25 [1] 12.5 13.5 Exercise: compute a confidence interval • Given the values of m (mean), and se (standard error), construct a vector containing the two values, 𝑛 ± 2 × 𝑡𝑓 . • That is, add and subtract two times the standard error value to/from the mean value. • Your output should look like: somgen223.stanford.edu 31

  32. ## one way: c (m - 2 * se, m + 2 * se) [1] 12.5 13.5 ## another way: m + c ( - 2, 2) * se [1] 12.5 13.5 Answer: compute a confidence interval somgen223.stanford.edu 32

Recommend


More recommend