Sie sind auf Seite 1von 11

A brief Review on some elementary commands R – ANLY500

This file briefly describes some elementary commands of R programming. You still need to learn it by
yourself because this file just wants you to get familiar with first steps of R programming.
The first step is to install R. You should go to this address to install the R software: https://www.r-
project.org

Now go to CRAN section and based on your location choose one of the servers which are nearer to you:
Choose your OS for installing the software (in the following picture the link of the Windows is shown):

After installing the R, you may install an IDE to make your programming faster and more convenient. My
suggestion is R-studio which you can download it here: www.rstudio.com

Click on the shown link which will lead you to the next page
You can choose the free version of software and start downloading and installing it. Now run the software
and try to get familiar with the environments. There is a brief description in your first lab about it.
The first important point about R is that R is sensitive to upper and lower case letters so pay attention to
that when you are typing them. Console, is the part that you can type your commands. To assign a number
or string to a variable you can use either = or <-:

You can see your variables and the values in upper right window (Environment):
You can also see a list of the variables with ls() command.
Several mathematical equations are predefined in R such as sqrt, log, exp, abs. To see how to use any
command in R you can type either ? or help in front of the command:

You can see the result in lower right window.


To define a vector you can use this command

On each step by typing the name of variable you can see the values assigned to t:

Other methods of sequence definition are as below (using : or seq command)

We can repeat a number several times using rep command. For example below you can see that number 1
is repeated 5 times.

Let’s define y as

We can address any index of y using [] in front of the variable. For example to read the third value of vector
y we can write:

Try to put some negative values and see the result. I mean y[-3]. Can you explain what did happen?
This addressing can be in form of logic too. For example, if we want to see all the values of y which are
less than 8 we can write:

We can define matrices too. Here is how it works:

Use help command to find out how this command is defined.


One important command is reading a database into R. There are several command for this. Read.csv is used
in your first lab. Here I will more talk about read.table command. The text database table is already posted
to your Moodle.

An easier way is to use file.choose so you can choose the location of file yourself.

We can see the dimension of imported data by:

For seeing start and end part of Data, you can use head and tail commands. names command on the other
way is used to show the header of the table.

To address a specific column in a table, we should use $ command. For example if we want to show the
mean of Age column in provided table we can write:

An easier way of pointing to columns is to attach the table to memory. This can be done by attach command.
We can remove the table from memory by detach command.

Using this command, for finding the mean, you can easily type:

At this point you know many commands for beginning playing around R. Now I just give you some ideas
about other commands and you should work on them and other commands to get better idea about R.
Try some other commands and see the effects on table. My suggestions are: calss(Age), levels(Gender),
summary(Data) and so on.
You can add a column to your table using cbind command.
To get the working directory and to modify it you can use getwd and setwd commands respectively.
Like other scripting languages, here you can write scripts. To start creating new R script, see below picture.
You can see a sample R script below. The first 2 lines are commented out. Line 3 deletes all the variables
and line 4 reads a table and stores it in Data variable.

To run this script use Source on top of this page and to run a particular line of the script, choose that line
and use Run key. Both keys are pointed by an arrow in above picture.
Last but not least is to work on plots and graphs on R. We can store a specific column by table command.

To have a better view let’s store these numbers in percentage format. To do so, we can devide all the values
in the table to the total number of participants (rows) which is 725 in this example.

Now we can see the bar plot by:

You can see the result in lower right box:


We can make our plot nicer by adding title and axis labels:

And here is the result. Could you see the effect of las=1?
Use pie command to see the pie chart. Or quantile to see the quantiles. If you have any issue, use help to
find out the correct way of calling the command.
We can also see the histogram of data with hist command.
We can see the density function mapped on this data by:
This is the end of short survey on R programming. As I stated earlier, this is just a brief review on some
elementary commands. You still need to refer to other references for complete review on R. I will explain
the commands which are needed for Labs.
This is a good website for beginners:
https://www.tutorialspoint.com/r/
The table provided here and review are from:
http://www.statslectures.com/index.php/r-stats-datasets

Das könnte Ihnen auch gefallen