WEBVTT

00:00:04.000 --> 00:00:06.970
You should have already
downloaded and installed

00:00:06.970 --> 00:00:09.060
R on your computer.

00:00:09.060 --> 00:00:12.440
If you have not downloaded
and installed R yet,

00:00:12.440 --> 00:00:15.150
please follow the instructions
on the course website

00:00:15.150 --> 00:00:18.330
before continuing
with this lecture.

00:00:18.330 --> 00:00:22.050
Once you've installed R,
go ahead and start it.

00:00:22.050 --> 00:00:26.170
You should see the R
console like we see here.

00:00:26.170 --> 00:00:28.910
This is where we'll type
commands and perform

00:00:28.910 --> 00:00:31.440
data analysis.

00:00:31.440 --> 00:00:35.460
In this course, we'll mostly
be working in the R console,

00:00:35.460 --> 00:00:37.810
but sometimes you'll
also want to use

00:00:37.810 --> 00:00:40.840
what are known as
"script files."

00:00:40.840 --> 00:00:43.100
We'll see an example
of how to use a script

00:00:43.100 --> 00:00:46.770
file at the end of this lecture.

00:00:46.770 --> 00:00:51.130
In your R console, you'll always
type commands after the arrow,

00:00:51.130 --> 00:00:53.510
or "greater than" sign.

00:00:53.510 --> 00:00:56.700
Let's start with some
basic calculations.

00:00:56.700 --> 00:00:59.130
So with your cursor
at the arrow,

00:00:59.130 --> 00:01:04.019
let's type 8, and then the
star symbol for "times,"

00:01:04.019 --> 00:01:06.870
the number 6, and hit Enter.

00:01:06.870 --> 00:01:11.360
You should see the
result 48, or 8 times 6.

00:01:11.360 --> 00:01:16.010
In this way, R can be used
as a basic calculator.

00:01:16.010 --> 00:01:20.420
We can perform many other
basic calculations, like 2,

00:01:20.420 --> 00:01:24.130
"raised to the power," by typing
the carrot symbol, and then

00:01:24.130 --> 00:01:27.070
the number 16,
and hitting Enter.

00:01:27.070 --> 00:01:35.240
You should see the result
65,536-- or 2 the power 16.

00:01:35.240 --> 00:01:38.430
Note that each of the results
here are labeled with a 1

00:01:38.430 --> 00:01:40.410
in brackets.

00:01:40.410 --> 00:01:43.430
This is just R's way
of labeling the output

00:01:43.430 --> 00:01:46.840
and we can safely ignore it.

00:01:46.840 --> 00:01:48.990
If you type something
in your R console

00:01:48.990 --> 00:01:52.490
but don't finish it
properly-- for example,

00:01:52.490 --> 00:01:55.450
try typing 2 and
the carrot symbol,

00:01:55.450 --> 00:01:59.880
and hitting Enter-- R
will show you a plus sign

00:01:59.880 --> 00:02:02.740
and will wait for you
to finish the command.

00:02:02.740 --> 00:02:05.660
You can either finish the
command-- in this case,

00:02:05.660 --> 00:02:09.669
by typing a number--
or you can hit Escape,

00:02:09.669 --> 00:02:13.680
and R will take you
back to the arrow sign.

00:02:13.680 --> 00:02:16.850
So if you ever see a plus
sign while working in R,

00:02:16.850 --> 00:02:20.430
it means that R is waiting
for you to finish the line.

00:02:20.430 --> 00:02:22.240
If you're not sure
how to finish it,

00:02:22.240 --> 00:02:25.710
you can always just hit Escape.

00:02:25.710 --> 00:02:27.900
A nice feature of
the R console is

00:02:27.900 --> 00:02:31.020
that you can scroll through
your previous commands

00:02:31.020 --> 00:02:35.060
by using the up and down
arrows on your keyboard.

00:02:35.060 --> 00:02:38.650
If you hit the up
arrow three times,

00:02:38.650 --> 00:02:43.110
you should go back to
the 8 times 6 command.

00:02:43.110 --> 00:02:47.079
If you hit Enter, you can
run this command again.

00:02:47.079 --> 00:02:50.540
You could also adjust
previous commands.

00:02:50.540 --> 00:02:54.770
Hit the up arrow again to go
back to the 8 times 6 command.

00:02:54.770 --> 00:03:00.950
This time, delete the 6,
type a 10, and hit Enter.

00:03:00.950 --> 00:03:03.130
We'll use this
approach to re-run

00:03:03.130 --> 00:03:05.930
or adjust commands many
times in this class.

00:03:08.510 --> 00:03:14.180
Generally, R works in terms
of functions and variables.

00:03:14.180 --> 00:03:18.470
A function can take in
several arguments, or inputs,

00:03:18.470 --> 00:03:21.840
and returns an output value.

00:03:21.840 --> 00:03:27.060
An example is the square
root, or sqrt, function.

00:03:27.060 --> 00:03:31.640
In your R console,
type sqrt, and then

00:03:31.640 --> 00:03:36.260
in parentheses, the
number 2, and hit Enter.

00:03:36.260 --> 00:03:43.140
The function is sqrt, the input,
or argument, is the number 2,

00:03:43.140 --> 00:03:49.150
and the output is 1.414214.

00:03:49.150 --> 00:03:52.850
There are thousands of
functions in R. Some of them

00:03:52.850 --> 00:03:55.620
are built into R--
like this one--

00:03:55.620 --> 00:03:59.050
and some can be added in by
installing packages, which

00:03:59.050 --> 00:04:03.000
we'll do several
times in this class.

00:04:03.000 --> 00:04:07.570
Another example of a function
is the abs, or absolute value

00:04:07.570 --> 00:04:11.950
function, which returns the
absolute value of a number.

00:04:11.950 --> 00:04:18.079
So if we type abs and then
in parentheses negative 65

00:04:18.079 --> 00:04:24.440
and hit Enter, we should get
the result 65 as our output.

00:04:24.440 --> 00:04:27.480
You can get help on
any function in R

00:04:27.480 --> 00:04:31.420
by typing a question mark
and then the function name.

00:04:31.420 --> 00:04:36.940
So if we type ?sqrt
and hit Enter,

00:04:36.940 --> 00:04:42.730
you should see the R Help
Page for the sqrt function.

00:04:42.730 --> 00:04:45.320
The help pages are
often very useful,

00:04:45.320 --> 00:04:47.420
and if you want to learn
more about a function,

00:04:47.420 --> 00:04:51.990
you should refer to
the help page in R.

00:04:51.990 --> 00:04:56.490
Now, get rid of the Help Page,
and go back to your R console.

00:04:56.490 --> 00:05:00.870
Suppose we now want to save
the output of a function.

00:05:00.870 --> 00:05:04.420
We can do this by
saving it to a variable.

00:05:04.420 --> 00:05:11.130
In your R console, type
SquareRoot2 and then an

00:05:11.130 --> 00:05:17.520
equals sign, and then sqrt, and
in parentheses the number 2.

00:05:17.520 --> 00:05:19.340
And hit Enter.

00:05:19.340 --> 00:05:23.360
Now you don't see
the output of sqrt(2)

00:05:23.360 --> 00:05:27.980
because we saved it to the
variable named "SquareRoot2."

00:05:27.980 --> 00:05:31.490
You can see the value of a
variable by typing its name

00:05:31.490 --> 00:05:32.840
and hitting Enter.

00:05:32.840 --> 00:05:38.220
So type SquareRoot2 exactly how
you typed it before, and hit

00:05:38.220 --> 00:05:39.340
Enter.

00:05:39.340 --> 00:05:45.900
You should see that it
takes the value 1.414214.

00:05:45.900 --> 00:05:48.470
SquareRoot2 is a
name that we created,

00:05:48.470 --> 00:05:51.760
and we could have named
it many other things.

00:05:51.760 --> 00:05:53.620
Generally, you have
a lot of freedom

00:05:53.620 --> 00:05:57.930
in naming your variables, but
there are a couple basic rules.

00:05:57.930 --> 00:06:02.290
One is that you should not
use spaces in variable names.

00:06:02.290 --> 00:06:06.060
If you want to easily separate
words in a variable name,

00:06:06.060 --> 00:06:09.580
popular strategies are using
a mix of capital and lowercase

00:06:09.580 --> 00:06:12.400
letters-- as we did
here-- or to separate

00:06:12.400 --> 00:06:15.290
the words using periods.

00:06:15.290 --> 00:06:17.510
Another basic rule is
that you should not

00:06:17.510 --> 00:06:20.500
start variable
names with a number.

00:06:20.500 --> 00:06:24.310
Keep in mind that variable
names in R are case-sensitive.

00:06:24.310 --> 00:06:29.590
Capital and lowercase letters
are seen differently by R.

00:06:29.590 --> 00:06:32.570
When we created our
variable SquareRoot2,

00:06:32.570 --> 00:06:35.250
we used the equals
sign for assignment,

00:06:35.250 --> 00:06:39.290
or to assign the
value of sqrt(2)

00:06:39.290 --> 00:06:42.440
to the variable
named SquareRoot2.

00:06:42.440 --> 00:06:46.310
You could instead use a
"less than" sign with a dash.

00:06:46.310 --> 00:06:52.860
For example, let's create a
new variable called HoursYear,

00:06:52.860 --> 00:06:56.740
and then type the less
than sign and a dash,

00:06:56.740 --> 00:07:01.280
followed by 365 times 24.

00:07:01.280 --> 00:07:06.210
If you hit Enter and then type
HoursYear to look at its value,

00:07:06.210 --> 00:07:12.280
you should see 8,760-- or the
number of hours in a year.

00:07:12.280 --> 00:07:15.560
By doing this, the less
than sign and the dash

00:07:15.560 --> 00:07:17.660
did the exact same
thing as it would

00:07:17.660 --> 00:07:20.580
have done if we used
the equals sign.

00:07:20.580 --> 00:07:23.820
In this course, we'll
typically use the equals sign.

00:07:23.820 --> 00:07:28.080
But if you see this less
than sign and a dash used,

00:07:28.080 --> 00:07:32.480
keep in mind that it means the
same thing as an equals sign.

00:07:32.480 --> 00:07:36.830
Lastly, if you type ls and then
empty parentheses in your R

00:07:36.830 --> 00:07:39.409
console and hit
Enter, you should

00:07:39.409 --> 00:07:41.620
see a list of all
of the variables

00:07:41.620 --> 00:07:45.180
that you've created in
your current R session.

00:07:45.180 --> 00:07:47.780
Here, we've created
two variables--

00:07:47.780 --> 00:07:50.780
HoursYear and SquareRoot2.

00:07:50.780 --> 00:07:53.570
This can be useful if you
forget exactly how you

00:07:53.570 --> 00:07:56.409
typed a variable name.

00:07:56.409 --> 00:08:00.110
These are our basic building
blocks in R-- functions,

00:08:00.110 --> 00:08:04.180
like square root and absolute
value, and variables,

00:08:04.180 --> 00:08:07.320
like HoursYear and SquareRoot2.