Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Bachelor of Economics

Ong Sheue-Li
ongsl@um.edu.my
Learning to program is an essential part of the education of every student,
not just in the sciences and engineering, but in the arts, social sciences, and
humanities, as well. Beyond direct applications, it is the first step in
understanding the nature of computer science’s undeniable impact on the
modern world. This course aims to teach programming in a scientific
context. The course begin by introducing basic programming elements such
as variables, conditionals, loops, arrays, and Input/Output. Next, this course
turns to functions, introducing key concepts such as recursion, modular
programming, and code reuse. Then, this course presents a modern
introduction to object-oriented programming.

2
At the end of the course, students are able to:
1. Perform basic programming using open source languages;
2. Use vectors, matrices, factors, data frames, and lists for programming;
and
3. Execute repetitive function using control looping.

3
Cotton, R. (2013). Learning R: A Step-by-Step
Function Guide to Data Analysis. O’Reilly Media, 1st
Edition.

4
5

Tells you how to install R and where to get help.


After reading this chapter, you should:
1.1 Know some things that you can use R to do
1.2 Know how to install R and an IDE to work with it
1.3 Be able to write a simple program in R
1.4 Know how to get help in R

6
7
 R is a language and environment for statistical computing and graphics
 R was developed in the early 1990s by Ross Ihaka and Robert Gentleman
 R was developed based on S language

8
 R is open source and free!
 R is vector based
 R is a programming language meant for statistics
 R can be integrated with other software such as SAS, SPSS, Excel

9
10
 R is more a programming language than an application. When you
download R, you automatically download a console application that’s
suitable for your operating system. However, this application has only
basic functionality, and it differs to some extent from one operating system
to the next.
 RStudio is a cross‐platform application, also known as an Integrated
Development Environment (IDE) with some very neat features to support
R.
 RStudio provides a common user interface across the major operating systems.
For this reason, we use RStudio to demonstrate some of the concepts rather
than any specific operating‐system version of R.

11
To Install R
1. Open an internet browser and go to www.r-project.org.
2. Click the "download R" link in the middle of the page under "Getting Started."
3. Select a CRAN location (a mirror site) and click the corresponding link.
4. Click on the "Download R for (Mac) OS X" link at the top of the page.
5. Click on the file containing the latest version of R under "Files."
6. Save the .pkg file, double-click it to open, and follow the installation instructions.
7. Now that R is installed, you need to download and install RStudio.
To Install RStudio
1. Go to www.rstudio.com and click on the "Download RStudio" button.
2. Click on "Download RStudio Desktop."
3. Click on the version recommended for your system, or the latest Mac version, save the .dmg file
on your computer, double-click it to open, and then drag and drop it to your applications folder.

12
To Install R
1. Open an internet browser and go to www.r-project.org.
2. Click the "download R" link in the middle of the page under "Getting Started."
3. Select a CRAN location (a mirror site) and click the corresponding link.
4. Click on the "Download R for Windows" link at the top of the page.
5. Click on the "install R for the first time" link at the top of the page.
6. Click "Download R for Windows" and save the executable file somewhere on your computer. Run
the .exe file and follow the installation instructions.
7. Now that R is installed, you need to download and install RStudio.
To Install RStudio
1. Go to www.rstudio.com and click on the "Download RStudio" button.
2. Click on "Download RStudio Desktop."
3. Click on the version recommended for your system, or the latest Windows version, and save the
executable file. Run the .exe file and follow the installation instructions.
13
14
 To open RStudio, click the RStudio icon in your menu system or on your
desktop.
 Once RStudio starts, choose File ➪ New File ➪ R Script to open a new
script file.

15
Pane 1: Pane 3:
Source Environment
and History

Pane 4:
Files, Plots,
Package,
Pane 2:
Help and
Console
Viewer

16
Panes Function
Source Can enter multiple lines of code, save your script file to
disk, and perform other tasks on your script.
Console This is where you do all the interactive work with R.
Environment • Can inspect the variables you created in your session, as
and History well as their values.
• This is also the area where you can see a history of the
commands you’ve issued in R.

17
Panes Function
Files, plots, • Files: This is where you can browse the folders and files
package, on your computer.
help and • Plots: This is where R displays your plots (charts or
viewer graphs).
• Packages: You can view a list of all installed packages. A
package is a self‐contained set of code that adds
functionality to R, similar to the way that add‐ins add
functionality to Microsoft Excel.
• Help: This is where you can browse R’s built‐in Help
system.
• Viewer: This is where RStudio displays previews of some
advanced features, such as dynamic web pages and
presentations that you can create with R and add‐on
packages.
18
Saying hello to the world
• Start a new R session, type “Hello World!” in your console, and press
Enter, R responds immediately with this output:
> print("Hello World!")
[1] "Hello World!"

• The code you type is called a command, because it will command your
computer to do something for you. The line you type it into is called the
command line.

19
Comments
> # This is a comment in R
>1+1 # Testing purpose
[1] 2

 R treats the hashtag character, #, in a special way; R will not run anything
that follows a hashtag on a line.
 This makes hashtags very useful for adding comments and annotations
to your code.
 If you’re writing large or complicated chunks of code in the editor, this
kind of annotation can be helpful to others (and indeed yourself!) who
want to understand what your code is doing.
 The hashtag is known as the commenting symbol in R.

20
Doing simple math
 The symbols +, ‐, *, and / are all operators, and they have the same
meaning they do in mathematics
>1+2+3+4+5
[1] 15
>6-4
[1] 2
>8/2
[1] 4
>3*2
[1] 6

 It’s good practice to always surround the operators with spaces. This
makes your code much easier to read and understand.
21
Using vectors
 A vector is the simplest type of data structure in R. The R manual defines
a vector as “a single entity consisting of a collection of things”.
 Vectors are generally created using the c() function.
1
 To construct a vector 2 :
3

> c (1, 2, 3)
[1] 1 2 3

 The entries inside the parentheses are referred to as arguments.

22
Sequence
 Sequence is a very handy operator, using a colon :

>1:3
[1] 1 2 3
> c(1 : 3)
[1] 1 2 3
> sum(1 : 3)
[1] 6
 More complex sequences can be created using the seq() function, like
defining number of points in an interval, or the step size.

> seq(1 , 2 , by=0.2)


[1] 1.0 1.2 1.4 1.6 1.8 2.0
23
>1+1
[1] 2
> 100 : 130
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
[17] 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
 You’ll notice that a [1] appears next to your result. R is just letting you
know that this line begins with the first value in your result.
 Some commands return more than one value, and their results may fill
up multiple lines.
 For example:
The command 100:130 returns 31 values; it creates a sequence of
integers from 100 to 130. Notice that new bracketed numbers appear
at the start of the second lines of output. These numbers just mean
that the second line begins with the 17th value in the result.
 You can mostly ignore the numbers that appear in brackets.
24
Storing and calculating values
 In R, the assignment operator is <‐, which you type in the console by
using two keystrokes: the less‐than symbol (<) followed by a hyphen (‐).
The combination of these two symbols represents assignment.

> x <- 1 : 5
>x
[1] 1 2 3 4 5
> y <- 10
>x+y
[1] 11 12 13 14 15
> z <- x + y
>z
[1] 11 12 13 14 15

25
> h <- hello
Error: object 'hello' not found
> h <- "hello"
>h
[1] "hello"

 Variables also can take on text values. You can assign the value "Hello" to
a variable called h.
 However, to assign text values to a variable, you must enter text or
character values to R inside quotation marks — either single or double. R
accepts both.
 So both h <‐ "Hello" and h <‐ 'Hello' are examples of valid R syntax.

26
 In “Using vectors,” earlier in this chapter, you use the c() function to
combine numeric values into vectors. This technique also works for text:
> hw <- c("Hello","world!")
> hw
[1] "Hello" "world!"

 You use the paste() function to concatenate multiple text elements. By


default, paste() puts a space between the different elements, like this:

> paste("Hello","world!")
[1] "Hello world!"

27
 You can name a variable in R almost anything you want, but there are a
few rules. First, a name cannot start with a number. Second, a name
cannot use some special symbols, like ^, !, $, @, +, -, /, or *:
> @y <- 2
Error: unexpected '@' in "@"
> 1y <- 2
Error: unexpected symbol in "1y"

 R also understands capitalization (or is case-sensitive), so name and


Name will refer to different variables.
 Finally, R will overwrite any previous information stored in a variable
without asking you for permission. So, it is a good idea to not use names
that are already taken.

28
Talking back to the user
 You can write R scripts that have some interaction with a user.
 To ask the user questions, you can use the readline() function. In the
following code snippet, you read a value from the keyboard and assign it
to the variable yourname:

> h <- "hello"


>h
[1] "hello"
> yourname <- readline("What is your name?")
What is your name? Sheue Li
> paste (h, yourname)
[1] "hello Sheue Li"

29
 We can issue individual commands in an interactive style of coding in the
R console. Meanwhile, we also can perform several commands one after
the other without waiting for additional instructions in the R source.
 To prepare your script to be sourced, you first write the entire script in an
editor window. Whenever you press Enter in the editor window, the
cursor moves to the next line, as in any text editor.
 To create a new script in RStudio, begin by opening the editor window
(choose File ➪ New File ➪ R script to open the editor window).

30
h <- "Hello"
yourname <- readline("what is your name?")
print (paste (h, yourname))
 Remember to type the print() function as part of your script.
 In interactive mode, a result is printed without needing to use a print()
function. But when you source a script, output is by default printed
only if you have an explicit print() function.
 You can type multiple lines of code into the source editor without having
each line evaluated by R. Then, when you’re ready, you can click Source
button and send the instructions to R.
 You can also send an individual line of code from the editor to the
console. Click the line of code you want to run, and then click the Run
button.

31
 You can run a highlighted portion of code in your script if you click the
Run button at the top of the scripts pane.
 You can run the entire script by clicking the Source button.
32
 When you create a variable, the variable will appear in the environment
pane of RStudio

33
34
Option 1: To get help on any function, type ? in the console
Option 2: Type help, but remember to use parentheses around your
search term

35
 Example: To get help with the paste () function:

> ?paste
> help(paste)

36
37
1. What is the command to create a vector of the numbers from 8 to 27
that progresses in steps of 0.4?
2. The function sd calculates the standard deviation. Calculate the standard
deviation of the numbers from 0 to 200.
3. What is the name of the function used to search for help within R?

38

You might also like