R programming
2019 Handout on Using R Math 152 Calc II By Cindy Bea Name:
The goal of this handout is to allow you to install R computer software on a Windows-based PC. R is an open-source software program designed for beginning learners. It focuses on statistics, calculus and linear algebra.
I. Installing R The install file can be found at the following location: R Cran Project If you are running Windows Vista, there is a link just for Vista/7/8. (that’s the one you want). Follow the on-screen instructions to install R into a folder. I use mirror (BC Canada). The default folder will be: “C:\Program Files\R\ “ or “C:\Program Files (x86)\R\”. It will create a link on the desktop called “R”. II Running R for the first time Double click the icon on the desktop and you will see the following:
This is the R program. When you run code that you have written you will paste it into this “working” screen. R works by the question and answer model. You enter a line with a command and press Enter. R then runs the code you wrote. When R is ready it gives you a prompt Basic Computing in R: Part I Numeric Decimal values are called numerics in R. It is the default computational data type. If we assign a decimal value to a variable x as follows, x will be of numeric type. x = 10.5 # assign a decimal value
x # print the value of x
[1] 10.5
R can act just like a calculator. Try the following:
2+2 [1] 4
Once we start writing our own code, we will start with simple 3 lines of code, then extend it to what we need very slowly. One way to work without being in the program is to pull up a “piece of paper”. In R this is called a script
Opening a Script: One of the first things we’ll do is to open a work page, in R this is called a script: File > new script>>
Within the script, whatever you type will run in the program. If you want to write notes about what you’re doing or what you hope to do, use # in front. If it is code such as x = rep(o,5), then it will run when you execute it. TO EXECUTE THE CODE: Highlight>> right click >> select RUN IN LINE Include the title, and any descriptions you think best to describe what you’re doing.
Basic Computing in R: Part II Vector
A vector is a sequence of data elements of the same basic type. Members in a vector are officially
called components. Nevertheless, we will just call them members in this site.
Here is a vector containing three numeric values 2, 3 and 5.
x = c(2, 3, 5)
x
2 3 5
Vector Index We retrieve values in a vector by declaring an index inside a single square bracket " [ ]" operator.
For example, the following shows how to retrieve a vector member. Since the vector index is 1-based, we use the
index position 3 for retrieving the third member.
s = c(2,3,4,5,6,7)
s[3]
[1] 4
Range Index To produce a vector slice between two indexes, we can use the colon operator ":". This can be convenient for
situations involving large vectors.
s = c("aa", "bb", "cc", "dd", "ee")
s[2:4]
[1] "bb" "cc" "dd"
Combining vectors: cbind and rbind B = c(2,4,3) and C = c(1,5,7)
Then we can combine the columns of B and C with cbind.
cbind(B, C) rbind(B,C)
[,1] [,2]
[1,] 2 1 B 2 4 3
[2,] 4 5 C 1 5 7
[3,] 3 7
Vector Arithmetics Arithmetic operations of vectors are performed member-by-member, i.e., memberwise. For example, suppose we have two vectors a and b. > a = c(1, 3, 5, 7)
> b = c(1, 2, 4, 8)
> a + b [1] 2 5 9 15
Combining Vectors:
Vectors can be combined via the function c. For examples, the following two vectors n and s are combined into a
new vector containing elements from both vectors.
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> c(n, s)
[1] "2" "3" "5" "aa" "bb" "cc" "dd" "ee"
Sequence Command: great way to define domains, x. seq(low,high, increment) Example: x= seq(1,10,2) x 1,3,5,7,9
Plot Command: creates a basic (x,y) plot x = seq(1,10,.5) y = 2*x + 3 plot(x,y)
Par function puts plots on the same page Putting two plots on the same paper: par (partitions the paper by mfrow (s) or mfcol (s) ) Par Command: par(mfrow = c( 2,2) ) gives 2 horizontal spaces and 2 vertical spaces [ * * ] [ * * ] par( mfrow = c(2,1) ) gives us 2 rows only 1 colum [ * ] ; par( mfrow = c(1,2) ) gives [* *] [ * ]
When you code, put par function first then the plots. The plots must follow right after!
Your first problem: [1]
1. Create a row vector using c ( ): called x with values { 2,3,…, 10} 2. Evaluate following lines: y1 = exp(x) and y2 = log(x) { in R: log( ) = ln( )} 3. Create and (x,y) graph using the ‘plot’ command as follows:
plot(x,y1) plot(x,y2) Comment on what you see: be sure to copy and paste each graph to your word document as they will disappear after each one shows. Turn in directions: cut and paste into a word doc:
1. Code you wrote 2. Results when you run it 3. Any graphs
Title, name, course as you would a paper.
Your Problem: You have already created domain, x. You would like to show two plots at the same time (using the par command). Using y1 = exp(x) and y2 = log(x)
1. Write the program to display both plot(x,y1) and plot(x,y2) on the same page using the par function
Turn in directions: Cut and paste as before into your Word doc That’s it! First experience with coding / done>>