A very first R–session Ulrich Halekoh † and Søren Højsgaard ‡ † Department of Animal Health and Bioscience, Aarhus University, Denmark ‡ Department of Mathematical Sciences, Aalborg University, Denmark August 19, 2012 Printed: August 19, 2012 File: firstsession-slides.tex
2: August 19, 2012 Contents 1 Basics of R 3 1.1 Numbers - R as a calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 Functions in R 6 2.1 Function arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3 Getting help 12 4 Installing and using R packages 15
3: August 19, 2012 1 Basics of R 1.1 Numbers - R as a calculator In R you can make simple calculations like R> (10 + 2) * 5 [1] 60 or apply mathematical functions like the natural logarithm R> log(100) [1] 4.60517
4: August 19, 2012 1.2 Assignment Often one wants to store the result in a variable (or, more appropriately, an object). This is done with an“assignment operator” . There are two assignment operators in R : <- and = . R> n <- (10 + 2) * 5 [1] 60 or equivalently: R> n = (10 + 2) * 5 [1] 60
5: August 19, 2012 Calculations can be made on objects, e.g: R> n^2 [1] 3600 R> log(n) [1] 4.094345 R> sin(n) [1] -0.3048106 R> n <- n + n + 10 [1] 130 Observe: If the object already exists, its previous value is overwritten. Observe: R is case–sensitive ( n and N are two different objects).
6: August 19, 2012 2 Functions in R R consists essentially of a collection of functions. A function is a rule that takes some input arguments into to some output. We have already seen several functions at work: + , * , log , sin and ^ . The typical call of a function is to type the functions name followed by the input arguments in parenthesis like R> log(123) [1] 4.812184
7: August 19, 2012 We have also seen exceptions to this rule, namely R> 123 + 234 [1] 357 However there is really a function call at work here. The function is called ` + ` (notice the back-quotes): R> ` + ` (123,234) [1] 357
8: August 19, 2012 2.1 Function arguments To see the type of arguments that go into a function we may use the args() function: R> args(log) function (x, base = exp(1)) NULL R> args(sin) function (x) NULL R> args( ' + ' ) function (e1, e2) NULL
9: August 19, 2012 We see that log() actually takes two arguments: x and base . The second argument is assigned a default value. R> exp(1) [1] 2.718282 which means that the log() function by default calculates the natural logarithm. If we want the logarithm in base 10 we do R> log(123, base=10) [1] 2.089905 We may verify the result as R> 10^log(123, base=10) [1] 123
10: August 19, 2012 The function rnorm() generates a random sample from a normal distribution. To see the input arguments: R> args(rnorm) function (n, mean = 0, sd = 1) NULL The second and third arguments have been assigned default values, so these need not to be specified. The first argument has no default value, so it must be specified: R> rnorm( n = 4) [1] -0.05841193 0.73691489 1.11769966 -0.89322232 To sample from a normal with mean 5 and standard deviation 1, we can do R> rnorm(n = 4, mean = 5) [1] 4.615602 7.046610 6.019073 5.345521
11: August 19, 2012 It is also allowed simply to write R> rnorm(4, 5) because R will match the given arguments with the order in which the function expects its arguments. If we want to sample with mean 0 and standard deviation 3, we can do one of the following: R> rnorm(n = 4, sd = 3) R> rnorm(4, 0, 3) It is generally recommended to be explicit about naming the arguments to functions, i.e. to write rnorm(n = 4,sd = 3) rather than rnorm(4, 0, 3) . Readability will be greatly enhanced – especially if returning to your work after a few months!
12: August 19, 2012 3 Getting help A very important step in learning a new program is knowing where to get help. In R there are several options. • If you know the name of the function you want to use: Use the manual pages to get detailed information. For example, the function rnorm generates a random sample from a normal distribution. To get help on rnorm : R> help(rnorm) This opens the manual page for rnorm . Note: at the bottom of the manual pages there are usually informative examples.
13: August 19, 2012 • If you do not know the name of the function you want: To get a list of functions with“norm”in its name, use R> apropos("norm") [1] "dlnorm" "dnorm" "norm" [4] "normalizePath" "plnorm" "pnorm" [7] "qlnorm" "qnorm" "qqnorm" [10] "qqnorm.default" "rlnorm" "rnorm" From here might make a guess. Alternatively, try R> help("Normal") • The R–help list: You may consider subscribing to the R-help mailing list, which is very active. Please do read the posting guide before sending a question to the list. Also, please remember that nobody is paid for answering questions on the list. Finally, please observe that any question asked on the list is available on the internet. • Search for key words or phrases in the R-help mailing list archives, or R manuals and help pages: R> RSiteSearch( ' normal distribution ' )
14: August 19, 2012 • It is recommended that you install Java on your computer. a With Java installed you can use a web-browser with the R help pages: R> help.start() • There are many good Wikis on the internet, for example: R Wiki: http://wiki.r-project.org/rwiki/ Quick-R home page: http://www.statmethods.net/ a Just search the web for“java download windows”using your favorite web search engine and you will be taken to the relevant web pages.
15: August 19, 2012 4 Installing and using R packages Much of Rs functionality is provided by the add–on packages created by a world–wide community of R-developers. These are not shipped with the R distribution but must be installed separately. To install e.g. the doBy package you can type R> install.packages("doBy") You will then be prompted to select a CRAN server (there are CRAN servers all over the world; pick one close to your location). Once the package is installed on your computer it must be“loaded”into R before it can be used: R> library(doBy)
16: August 19, 2012 Notice: A package must be loaded once per session with R , because the package is unloaded when R is shut down. It is not necessary to install the package in each R session. R packages are frequently updated on CRAN. Hence it is a good idea to ensure that you have the most recent versions on your computer. This can done by: R> update.packages() To see what is in the doBy package do R> help(package=doBy)
Recommend
More recommend