#intro.R #Population Biology , Fall 2011 BIOL 763 / SCCC 411B / Math 523 #OBJECTIVES: # # - Enter mathematical expressions # - Plot functions and extract information from graphs # - Learn the solve and other commands for algebra and calculus # #WHAT IS R AND HOW CAN IT BE USED? # #It is probably a good idea to begin EVERY worksheet that you create with the following command. rm(list=ls()) #this clears the system memory #BASIC FACTS # #R is a computer language; it cannot read your mind. You need to learn how to communicate with Maple. # #The basic symbols: # - assignments are made with <- (plain = has a different meaning) -- think of this as giving the value of the right hand side to the name that appears on the left hand side # - commands can be terminated with a semicolon ( ; ) # - unlike Maple, there does not appear to be a command for "most recent result" # - R is case sensitive -- that is, the names x and X are different, pi and Pi are not the same thing # - [] -- selection operators for extracting subsets from vectors or matrices # - : -- as in a:b -- this is how R indicates a sequence of numbers from a to b # - ?topic or help(topic) -- a request to R for information #Mathematical functions have their standard names, or ones that are easily guessed: # + (plus) , - (minus) , * (times) , / (divided by) , ^ (raised to the power) , sin, cos, tan, abs, sqrt, ... #In the following lines, use paper and pencil to first predict what you think R will do. #Then execute the command and see what actually happens! N<- 4 * 6 + 12 / 6 - 1 N power<- (-3)^3 power pi # note that in R the numerical value is called "pi" and in Maple it is "Pi" v<- sin( pi / 4 ) v tan( -Pi / 2 ) 3 / ( 5 - sqrt( N ) ) factorial(4) # note the difference from Maple, which uses the ! symbol to indicate factorial factorial(50) #computer algebra calculations can be done in R using the Ryacas package #yacas is "yet another computer algebra system" #as of 28-Aug-2011 I have not tried yacas or Ryacas. More information later. #PLOTTING t<- seq(-3,6,0.1) # create a sequence of numbers from -3 to 6 in increments of 0.1 plot(t,3*t-2) # the plot command is plot(x,y) where y is the vertical axis and x is the horizontal x<-seq(-2*pi,2*pi,0.1) plot(x,sin(3*x)) t<-seq(0,10,0.1) y<-3.5*exp(0.2*t) plot(t,y) L<-ln(y) plot(t,L) #We illustrate how to plot data points, and the usefulness of the logarithm function. pop<- c( 508, 711, 912, 1131, 1590, 1811, 2015, 2249, 2509, 3008, 3610, 3967) yr<- c( 1650, 1750, 1800, 1850, 1900, 1920, 1930, 1940, 1950, 1960, 1970, 1975) log10pop<-log10(pop) lnpop<- log(pop) adjustedyr<-yr - 1650 plot(yr,pop,main="Population vs time") plot(adjustedyr,pop,main="Population vs time",lwd=2, col="green") plot(adjustedyr,log10pop main="Log10 of Pop vs time") plot(adjustedyr,lnpop,main="Ln of pop vs time") #We have two ways to make logarithmic plots. #We can either take log of the data values, as we did above, or we can use the built-in R command. plot(adjustedyr,pop,log="y",main="Semilog plot of Pop vs time") #Here are a few more functions and their graphs x<-seq(-3,3,0.1) f<-sin(x); g<-2*x^2; p<-f*g; h<-cos((1/4)*x); s<-0.2*sin(2*x)+h; plot(x,p) #R can display the values on graphs via the locator() command. #Position the cursor on a point on the graph and click the left button. #When you are done, press the right button and select "stop". #The numbers are the coordinates of the current location of the cursor. #Use this technique to identify the maximum and minimum values of p(x) on the interval [-3, 3], and the x-values at which these are found. # locator() #To plot more than one set of data on a graph, use the points() or lines() command: x<-seq(-6*pi,6*pi, 0.1) f<-sin(x); g<-2*x^2; p<-f*g; h<-cos((1/4)*x); s<-0.2*sin(2*x)+h; plot(x,h) lines(s,h) x<-seq(-16,16,0.1) f<-sin(x); g<-2*x^2; p<-f*g; ymax<-max(g) #find the largest value that we are planning to plot ymin<-min(-g) #find the smallest value plot(x,g,ylim=c(ymin,ymax),type="l",col="red") lines(x,-g,col="blue") lines(x,p,col="black") #