Tip:
Highlight text to annotate it
X
This video covers key strategies and knowledge that
will make working in R much easier in the
near and long term.
Let's start by opening R. If we want, we can type directly
into the console.
However, this makes it a bit difficult to recreate our
work, so it's helpful to open a new R document, also called
an R script.
We can open a script by holding down Command and
hitting N.
Whenever you write R commands, you should write
them in an R script.
Then you can send the commands in your script to an R
console to be run.
For example, if your cursor is unaligned in the R script, you
can run that line of code in the console by holding down
Command and hitting Return.
To run a larger block of code, highlight the code, hold down
command and hit Return.
It is useful to type your commands into an R script
because you can organize your commands, and you can run and
rerun these commands quickly and easily.
You can also save an R script so you can use it later, and
this makes it easy to pick up where you left off in your
next work session.
Here, I'll save my script to the desktop with the name
temp.R. Saving and loading files in R can very helpful.
R is always looking in a particular folder on your
computer, and it is sometimes important to know where it is
looking so you can access and save files.
There are two tools that will make this easier--
getwd and setwd, which stand for get working directory and
set working directory.
The getwd function will tell you which directory R is
currently working from.
If you'd like to change the directory you're working in,
use the setwd function and specify a file path inside the
parentheses, where the path is contained in quotation marks.
For those working in the OS X, there's one more tool--
Command D. Typing Command D will open a window where you
can navigate to a folder of your choosing.
When you are done in an R session, save your script and
then type q open parentheses close
parentheses into the console.
As long you carefully document your work in an R script,
there's little reason to save your work space.
In the next video, we'll begin creating objects in R.