General Navigation in R

So you’ve finally managed to install the pesky environment but have no idea what you are looking at when you open the program. This tutorial is for you. (Again, here is a version with screenshot pictures).

When you open R, it might look different than the screenshots in the picture version of the tutorial. This depends on what type of computer you are using and how you installed R (look at step 8 of the installation post).

nav1

When you first open R, there should be a window with this information in it. The version of your copy of R can be found at the beginning (starred). Every time you open R, this window is called the workspace. A workspace can be likened to a box. You can put multiple things in the box and rearrange and analyze the objects in the box. Depending on how you installed R, you can have multiple workspaces/boxes open to put in and remove things. When you open R, you can have a new box or old box (saved workspace) opened depending on how you closed R. More information on that later.

Now we are going to try entering in the command “help.start()” which will open a help guide in Chrome, Firefox, Safari etc. In the previous screenshot, there is a box around the greater-than sign (“>”). This is where you write directions to R. Try typing “help.start()” exactly (without the quotation marks) and press the Enter/Return key.

nav2

*From now on, commands will be written like so

help.start()

When the command is finished running (R has heard and followed your instructions), another greater-than sign should appear. You would then be able to write another command and run it. (When you run scripts that require lots of time and computation, this sign will become a symbol of relief, happiness and many other positive emotions).  Depending on the command, R will spit out some output about what it is doing or results from the instructions before the greater-than sign appears. (Marked by an arrow in the following picture).
nav3

Here is that happy symbol of a successfully run code and then a help screen pops up. If you click on “An Introduction to R” there are a myriad of tutorials already written for R that were installed on your computer when you installed R.

nav5

Alternatively, you could get a PDF version of the guide by clicking on the top menu Help>Manuals (in PDF)>An Introduction to R. Although it is easy to look for answers using the all powerful tool Google, this PDF is a solid, 101-page introduction to R. A downloadable version from the internet can also be found here.

Although R is good at following instructions, it is pickier than a toddler choosing what to eat when it comes to written commands. You have to write commands exactly for them to be performed. So you  will not misspell, not change uppercased and lowercased letters and definitely not change a location of a period or underscore. If you do, R will whine and spit out an error message in response to your command.

Picky R doesn't like anything new

Picky R doesn’t like anything new

Let’s try some basic arithmetic. Remember to press enter between each line.

4+7
10/3
4-7+23
3*(20+1)
sqrt(144)
remember PEMDAS!

remember PEMDAS!

You can also create objects (now let’s remember algebRa) by assigning numbers or even other objects to the new object.

x<-3
y<-7
z<-x*y

nav9

If you type the name of these objects, you can see what each object contains.

x
y
z

nav10

To see all the objects you have in your workspace (or box, to continue the earlier metaphor):

ls()

nav11

You can reassign new contents to an object.

a<-1
a
a<-x
a
a<-200
a

nav12

An object can contain more than just one number. Here are some different versions of sequences.

x<-c(5,9)
y<-seq(1,21, by=2)
z<-seq(0,100, length=9)
w<-4:32
x
y
z
w

nav16

If you want to repeat a function you wrote earlier, press on the up or down buttons on your keyboard.

You can also erase all the objects in your workspace.

ls()
rm(list=ls())
ls()

nav11

To exit R, you can either close out of it or type

q()
nav15

How courteous of R

Either way, R will ask if you would like to save your workspace. If you choose to save, R will remember objects in the workspace even if you turn off your computer.

Here is a link to an excellent and shorter introduction to R (compared to the earlier one we opened up through R) if you would like to get more comfortable navigating in R.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s