1 00:00:04,000 --> 00:00:06,970 You should have already downloaded and installed 2 00:00:06,970 --> 00:00:09,060 R on your computer. 3 00:00:09,060 --> 00:00:12,440 If you have not downloaded and installed R yet, 4 00:00:12,440 --> 00:00:15,150 please follow the instructions on the course website 5 00:00:15,150 --> 00:00:18,330 before continuing with this lecture. 6 00:00:18,330 --> 00:00:22,050 Once you've installed R, go ahead and start it. 7 00:00:22,050 --> 00:00:26,170 You should see the R console like we see here. 8 00:00:26,170 --> 00:00:28,910 This is where we'll type commands and perform 9 00:00:28,910 --> 00:00:31,440 data analysis. 10 00:00:31,440 --> 00:00:35,460 In this course, we'll mostly be working in the R console, 11 00:00:35,460 --> 00:00:37,810 but sometimes you'll also want to use 12 00:00:37,810 --> 00:00:40,840 what are known as "script files." 13 00:00:40,840 --> 00:00:43,100 We'll see an example of how to use a script 14 00:00:43,100 --> 00:00:46,770 file at the end of this lecture. 15 00:00:46,770 --> 00:00:51,130 In your R console, you'll always type commands after the arrow, 16 00:00:51,130 --> 00:00:53,510 or "greater than" sign. 17 00:00:53,510 --> 00:00:56,700 Let's start with some basic calculations. 18 00:00:56,700 --> 00:00:59,130 So with your cursor at the arrow, 19 00:00:59,130 --> 00:01:04,019 let's type 8, and then the star symbol for "times," 20 00:01:04,019 --> 00:01:06,870 the number 6, and hit Enter. 21 00:01:06,870 --> 00:01:11,360 You should see the result 48, or 8 times 6. 22 00:01:11,360 --> 00:01:16,010 In this way, R can be used as a basic calculator. 23 00:01:16,010 --> 00:01:20,420 We can perform many other basic calculations, like 2, 24 00:01:20,420 --> 00:01:24,130 "raised to the power," by typing the carrot symbol, and then 25 00:01:24,130 --> 00:01:27,070 the number 16, and hitting Enter. 26 00:01:27,070 --> 00:01:35,240 You should see the result 65,536-- or 2 the power 16. 27 00:01:35,240 --> 00:01:38,430 Note that each of the results here are labeled with a 1 28 00:01:38,430 --> 00:01:40,410 in brackets. 29 00:01:40,410 --> 00:01:43,430 This is just R's way of labeling the output 30 00:01:43,430 --> 00:01:46,840 and we can safely ignore it. 31 00:01:46,840 --> 00:01:48,990 If you type something in your R console 32 00:01:48,990 --> 00:01:52,490 but don't finish it properly-- for example, 33 00:01:52,490 --> 00:01:55,450 try typing 2 and the carrot symbol, 34 00:01:55,450 --> 00:01:59,880 and hitting Enter-- R will show you a plus sign 35 00:01:59,880 --> 00:02:02,740 and will wait for you to finish the command. 36 00:02:02,740 --> 00:02:05,660 You can either finish the command-- in this case, 37 00:02:05,660 --> 00:02:09,669 by typing a number-- or you can hit Escape, 38 00:02:09,669 --> 00:02:13,680 and R will take you back to the arrow sign. 39 00:02:13,680 --> 00:02:16,850 So if you ever see a plus sign while working in R, 40 00:02:16,850 --> 00:02:20,430 it means that R is waiting for you to finish the line. 41 00:02:20,430 --> 00:02:22,240 If you're not sure how to finish it, 42 00:02:22,240 --> 00:02:25,710 you can always just hit Escape. 43 00:02:25,710 --> 00:02:27,900 A nice feature of the R console is 44 00:02:27,900 --> 00:02:31,020 that you can scroll through your previous commands 45 00:02:31,020 --> 00:02:35,060 by using the up and down arrows on your keyboard. 46 00:02:35,060 --> 00:02:38,650 If you hit the up arrow three times, 47 00:02:38,650 --> 00:02:43,110 you should go back to the 8 times 6 command. 48 00:02:43,110 --> 00:02:47,079 If you hit Enter, you can run this command again. 49 00:02:47,079 --> 00:02:50,540 You could also adjust previous commands. 50 00:02:50,540 --> 00:02:54,770 Hit the up arrow again to go back to the 8 times 6 command. 51 00:02:54,770 --> 00:03:00,950 This time, delete the 6, type a 10, and hit Enter. 52 00:03:00,950 --> 00:03:03,130 We'll use this approach to re-run 53 00:03:03,130 --> 00:03:05,930 or adjust commands many times in this class. 54 00:03:08,510 --> 00:03:14,180 Generally, R works in terms of functions and variables. 55 00:03:14,180 --> 00:03:18,470 A function can take in several arguments, or inputs, 56 00:03:18,470 --> 00:03:21,840 and returns an output value. 57 00:03:21,840 --> 00:03:27,060 An example is the square root, or sqrt, function. 58 00:03:27,060 --> 00:03:31,640 In your R console, type sqrt, and then 59 00:03:31,640 --> 00:03:36,260 in parentheses, the number 2, and hit Enter. 60 00:03:36,260 --> 00:03:43,140 The function is sqrt, the input, or argument, is the number 2, 61 00:03:43,140 --> 00:03:49,150 and the output is 1.414214. 62 00:03:49,150 --> 00:03:52,850 There are thousands of functions in R. Some of them 63 00:03:52,850 --> 00:03:55,620 are built into R-- like this one-- 64 00:03:55,620 --> 00:03:59,050 and some can be added in by installing packages, which 65 00:03:59,050 --> 00:04:03,000 we'll do several times in this class. 66 00:04:03,000 --> 00:04:07,570 Another example of a function is the abs, or absolute value 67 00:04:07,570 --> 00:04:11,950 function, which returns the absolute value of a number. 68 00:04:11,950 --> 00:04:18,079 So if we type abs and then in parentheses negative 65 69 00:04:18,079 --> 00:04:24,440 and hit Enter, we should get the result 65 as our output. 70 00:04:24,440 --> 00:04:27,480 You can get help on any function in R 71 00:04:27,480 --> 00:04:31,420 by typing a question mark and then the function name. 72 00:04:31,420 --> 00:04:36,940 So if we type ?sqrt and hit Enter, 73 00:04:36,940 --> 00:04:42,730 you should see the R Help Page for the sqrt function. 74 00:04:42,730 --> 00:04:45,320 The help pages are often very useful, 75 00:04:45,320 --> 00:04:47,420 and if you want to learn more about a function, 76 00:04:47,420 --> 00:04:51,990 you should refer to the help page in R. 77 00:04:51,990 --> 00:04:56,490 Now, get rid of the Help Page, and go back to your R console. 78 00:04:56,490 --> 00:05:00,870 Suppose we now want to save the output of a function. 79 00:05:00,870 --> 00:05:04,420 We can do this by saving it to a variable. 80 00:05:04,420 --> 00:05:11,130 In your R console, type SquareRoot2 and then an 81 00:05:11,130 --> 00:05:17,520 equals sign, and then sqrt, and in parentheses the number 2. 82 00:05:17,520 --> 00:05:19,340 And hit Enter. 83 00:05:19,340 --> 00:05:23,360 Now you don't see the output of sqrt(2) 84 00:05:23,360 --> 00:05:27,980 because we saved it to the variable named "SquareRoot2." 85 00:05:27,980 --> 00:05:31,490 You can see the value of a variable by typing its name 86 00:05:31,490 --> 00:05:32,840 and hitting Enter. 87 00:05:32,840 --> 00:05:38,220 So type SquareRoot2 exactly how you typed it before, and hit 88 00:05:38,220 --> 00:05:39,340 Enter. 89 00:05:39,340 --> 00:05:45,900 You should see that it takes the value 1.414214. 90 00:05:45,900 --> 00:05:48,470 SquareRoot2 is a name that we created, 91 00:05:48,470 --> 00:05:51,760 and we could have named it many other things. 92 00:05:51,760 --> 00:05:53,620 Generally, you have a lot of freedom 93 00:05:53,620 --> 00:05:57,930 in naming your variables, but there are a couple basic rules. 94 00:05:57,930 --> 00:06:02,290 One is that you should not use spaces in variable names. 95 00:06:02,290 --> 00:06:06,060 If you want to easily separate words in a variable name, 96 00:06:06,060 --> 00:06:09,580 popular strategies are using a mix of capital and lowercase 97 00:06:09,580 --> 00:06:12,400 letters-- as we did here-- or to separate 98 00:06:12,400 --> 00:06:15,290 the words using periods. 99 00:06:15,290 --> 00:06:17,510 Another basic rule is that you should not 100 00:06:17,510 --> 00:06:20,500 start variable names with a number. 101 00:06:20,500 --> 00:06:24,310 Keep in mind that variable names in R are case-sensitive. 102 00:06:24,310 --> 00:06:29,590 Capital and lowercase letters are seen differently by R. 103 00:06:29,590 --> 00:06:32,570 When we created our variable SquareRoot2, 104 00:06:32,570 --> 00:06:35,250 we used the equals sign for assignment, 105 00:06:35,250 --> 00:06:39,290 or to assign the value of sqrt(2) 106 00:06:39,290 --> 00:06:42,440 to the variable named SquareRoot2. 107 00:06:42,440 --> 00:06:46,310 You could instead use a "less than" sign with a dash. 108 00:06:46,310 --> 00:06:52,860 For example, let's create a new variable called HoursYear, 109 00:06:52,860 --> 00:06:56,740 and then type the less than sign and a dash, 110 00:06:56,740 --> 00:07:01,280 followed by 365 times 24. 111 00:07:01,280 --> 00:07:06,210 If you hit Enter and then type HoursYear to look at its value, 112 00:07:06,210 --> 00:07:12,280 you should see 8,760-- or the number of hours in a year. 113 00:07:12,280 --> 00:07:15,560 By doing this, the less than sign and the dash 114 00:07:15,560 --> 00:07:17,660 did the exact same thing as it would 115 00:07:17,660 --> 00:07:20,580 have done if we used the equals sign. 116 00:07:20,580 --> 00:07:23,820 In this course, we'll typically use the equals sign. 117 00:07:23,820 --> 00:07:28,080 But if you see this less than sign and a dash used, 118 00:07:28,080 --> 00:07:32,480 keep in mind that it means the same thing as an equals sign. 119 00:07:32,480 --> 00:07:36,830 Lastly, if you type ls and then empty parentheses in your R 120 00:07:36,830 --> 00:07:39,409 console and hit Enter, you should 121 00:07:39,409 --> 00:07:41,620 see a list of all of the variables 122 00:07:41,620 --> 00:07:45,180 that you've created in your current R session. 123 00:07:45,180 --> 00:07:47,780 Here, we've created two variables-- 124 00:07:47,780 --> 00:07:50,780 HoursYear and SquareRoot2. 125 00:07:50,780 --> 00:07:53,570 This can be useful if you forget exactly how you 126 00:07:53,570 --> 00:07:56,409 typed a variable name. 127 00:07:56,409 --> 00:08:00,110 These are our basic building blocks in R-- functions, 128 00:08:00,110 --> 00:08:04,180 like square root and absolute value, and variables, 129 00:08:04,180 --> 00:08:07,320 like HoursYear and SquareRoot2.