# The public domain program 'R' can be used as anything from # a simple calculator to a sophisticated programming environment. # We will be using it primarily as a platform for statistical # analysis. Today, we saw a few simple procedures designed to show # that R can be fairly simple and intuitive. # Here is a simple calculation: > 3 * 3 [1] 9 # While R can be used as a simple calculator, one must use caution # when working with large numbers: > 150000.30 + 150000.33 [1] 300000.6 # Functions in R tend to have short, intuitive names. For example, # the function to calculate a square root is: > sqrt(9) [1] 3 # Functions can be applied to more complex expressions. Here, # 3^2 means "three squared": > sqrt( 3^2 ) [1] 3 # It is simple to create a variable in R. Here, we create a # variable that we choose to name 'x' and stor the value 3 in it: > x <- 3 # (The assignment operator '<-' represents an arrow and is read 'gets.' # To see what value(s) a variable contains, simply type its name: > x [1] 3 # Basic operations can be performed on variables, just as we did # before with numbers: > x^2 [1] 9 > sqrt(x^2) [1] 3 # Sometimes, we may have typed a complex expression and only then # realized that we want to store it in a variable. In that case, # we can type the arrow in the left-to-right direction and still # save the result: > 3^2 -> y > y [1] 9 # To find out what variables or other objects you have in your # workspace, use the 'ls()' function: > ls() [1] "x" "y" # Variables can hold more than one value. (Variables with more than # one value are called vectors. At least for now; things can get more # complex than that.) Here, we create a variable x that contains the # integers from 1 to 10: > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 # We can perform operations on that vector, just as we could on # a single-valued variable (often called a scalar): > x^2 [1] 1 4 9 16 25 36 49 64 81 100 > sqrt(x) [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 [9] 3.000000 3.162278 # We can use the c() function to 'collect' a set of values: > x <- c( 2.2, 3.141592654, pi, 99, 3811) > x [1] 2.200000 3.141593 3.141593 99.000000 3811.000000 # To get rid of a variable, remove it, using the rm() function: > rm(x) # Notice that x is now gone: > ls() [1] "y" # We can get a set of integers in descending order this way: > x <- 10:1 > x [1] 10 9 8 7 6 5 4 3 2 1 # If we wanted them back in ascending order, we could use the # rev() function to "reverse" them: > rev(x) [1] 1 2 3 4 5 6 7 8 9 10 # Statistical functions also tend to have short and intuitive # names. For example, we can use the mean() function to calculate # the mean, which is just the arithmetic average: > mean(x) [1] 5.5 # The median is the centermost value or the average of the two central # value if there is not a unique central one. Not surprisingly, the # function to get it is median(): > median(x) [1] 5.5 # Sometimes, though, what seems like a natural, intuitive function # name does something else. For example, we might be interested in the # most frequently occurring value, also called the mode. But the # mode() function in R just tells us what kind of variable we have: > mode(x) [1] "numeric" # It is often beneficial to have a special workspace in R for # different projects. For example, my home R workspace looks like # this: > ls() [1] "anscombe" "bben" "Benford" [4] "bet" "Bexp" "bias" [7] "Checkouts" "cites" "classdots" [10] "classdotsoriginal" "culleddots" "d2ldm2" [13] "d2ldvc2" "d2ldvcdm" "degrees" [16] "digits" "dldm" "dldvc" [19] "expect" "Eysenck" "Eysenck2" [22] "fusion" "gatb" "glass2" [25] "gradient" "guesses" "hessian" [28] "i" "j" "justangles" [31] "justculledangles" "library" "meanangles" [34] "names" "no" "NYjobs" [37] "order" "pars" "pulse" [40] "pulse1" "pulse2" "q1a" [43] "q1m" "radians" "radius" [46] "rich" "rmse" "spaces" [49] "Statlab" "tbias" "temp0" [52] "temp1" "temp2" "theta" [55] "tmeanangle" "tmeanangles" "tmeanradii" [58] "trained" "trmse" "trueangle" [61] "trueradii" "Uexp" "untrained" [64] "utbias" "utmeanangles" "utmeanradii" [67] "utrmse" "V" "winnings" [70] "words" "x" "xx" [73] "xxx" "y" "yes" [76] "yy" "yyy" "Z" > # But if I want to have a workspace that contains just things that # are relevant to this class, I can specify a special working # directory for my R session: > setwd("c:/users/jvevea/desktop/Classes/202a") # I now remove everything in my workspace: > rm(list=ls()) # Note that all of that stuff is now gone: > ls() character(0) # When I quit, using the q() function, R will ask me if I want # to save my workspace. If I tell it yes, it will do so, creating # a little file that appears as an "R" in the folder that I specified # with my "setwd()" command. > q() # If I start R up again by double clicking on that icon, all # of the things I deleted from my workspace are still gone (and # any variable I created after the deletion would still be there): > ls() character(0) # But if I quit again and start R from my toolbar, all of # the variables in my home workspace are still there: > ls() [1] "anscombe" "bben" "Benford" [4] "bet" "Bexp" "bias" [7] "Checkouts" "cites" "classdots" [10] "classdotsoriginal" "culleddots" "d2ldm2" [13] "d2ldvc2" "d2ldvcdm" "degrees" [16] "digits" "dldm" "dldvc" [19] "expect" "Eysenck" "Eysenck2" [22] "fusion" "gatb" "glass2" [25] "gradient" "guesses" "hessian" [28] "i" "j" "justangles" [31] "justculledangles" "library" "meanangles" [34] "names" "no" "NYjobs" [37] "order" "pars" "pulse" [40] "pulse1" "pulse2" "q1a" [43] "q1m" "radians" "radius" [46] "rich" "rmse" "spaces" [49] "Statlab" "tbias" "temp0" [52] "temp1" "temp2" "theta" [55] "tmeanangle" "tmeanangles" "tmeanradii" [58] "trained" "trmse" "trueangle" [61] "trueradii" "Uexp" "untrained" [64] "utbias" "utmeanangles" "utmeanradii" [67] "utrmse" "V" "winnings" [70] "words" "x" "xx" [73] "xxx" "y" "yes" [76] "yy" "yyy" "Z" >