getting started with r · 2017. 7. 13. ·...
TRANSCRIPT
www.utm.my innova-ve ● entrepreneurial ● global 1
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
12 July 2017 "Dr. Norhaiza Ahmad"
Department of Mathematical Sciences"Faculty of Science"
Universiti Teknologi Malaysia"
Getting Started with R!for newbies!
!PART B: !
FUNCTIONS-PACKAGES-HELP!!12 July 2017 "
Dr. Norhaiza Ahmad"Department of Mathematical Sciences"
Faculty of Science"Universiti Teknologi Malaysia"
http://science.utm.my/norhaiza/!
www.utm.my innova-ve ● entrepreneurial ● global 2
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Outline
PART B: Core of R 1. R func-ons (in-‐built) 2. R Packages 3. How to search for Func-on/Packages etc
www.utm.my innova-ve ● entrepreneurial ● global 3
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Outline
PART B: Core of R 1. R funcNons (in-‐built) 2. R Packages 3. How to search for Func-on/Packages etc
www.utm.my innova-ve ● entrepreneurial ● global 4
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Func-ons in R
• R commands are executed by running a func-on • A func-on is a name, which is typed followed by a pair
of brackets. Arguments are added inside the brackets > sqrt(2) > sin(pi) • Some-mes func-ons in R have extra argument. > sum(2,3,5) > log(10,10)
• R has many in-‐built func-ons. • These func-ons can also be programmed manually
www.utm.my innova-ve ● entrepreneurial ● global 5
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Examples of Other Func-ons in R
FUNCTIONS OperaNon sqrt Square root abs Absolute value cos tan Trigonometric func-ons (radians) sinh cosh tanh hyperbolic func-ons Asinh acosh atanh inverse hyperbolic func-ons exp log log10
exponen-al ,natural logarithm common logarithm
• What are other func-ons in R?
• Where to find other func-ons?
These func-ons can be nested and combined to make more complex expressions:
> sqrt(sin(45*pi/180)) [1] 0.8408964
www.utm.my innova-ve ● entrepreneurial ● global 6
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Outline
PART B: Core of R 1. R func-ons (in-‐built) 2. R Packages 3. How to search for Func-on/Packages etc
www.utm.my innova-ve ● entrepreneurial ● global 7
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Packages in R ● a package is a collec-on of R func-ons, data and compiled
codes. ● Different packages cater for different purposes that can be
reused by others.
Every R package is simply a bundle of code that serves a specific purpose and is designed to be reusable by other developers. In addi-on to the primary codebase, packages o^en include data, documenta-on, and tests. As an R user, you can simply download a par-cular package (some are even pre-‐installed) and start using its func-onali-es. Everyone can develop R packages, and everyone can share their R packages with others. The above is an extremely powerful concept and one of the key reasons R is so successful as a language and as a community. Namely, you don’t need to do all the hard core programming yourself or understand every complex detail of a par-cular algorithm or visualiza-on. You can simple use the out-‐of-‐the box func-ons that come with the relevant package as an interface to such func-onali-es. As such it is useful to have an understanding of R’s package ecosystem. Many R packages are available from the Comprehensive R Archive Network, and you can install them using the install.packages func-on. What is great about CRAN is that it associates packages with a par-cular task via Task Views. Alterna-vely, you can find R packages on bioconductor, github and bitbucket. Looking for a par-cular package and corresponding documenta-on? Try Rdocumenta-on, where you can easily search packages from CRAN, github and bioconductor.
Powerful concept-‐ successful as a language community – No need to do heavy programming on your own – can use packages/func-ons freely
Concept: Anyone Can Develop à Share
R Package 2
R func-on 1
R func-on 2
R func-on n
R Package 2
R func-on 1
R func-on 2
R func-on n
Simple structure of package/func-ons in R in a nutshell
www.utm.my innova-ve ● entrepreneurial ● global 8
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Packages in R ● Different packages cater for different purposes that can be reused by others. Ø R comes with a pre-‐installed set of packages Ø There are many other packages that can be
downloaded.
Every R package is simply a bundle of code that serves a specific purpose and is designed to be reusable by other developers. In addi-on to the primary codebase, packages o^en include data, documenta-on, and tests. As an R user, you can simply download a par-cular package (some are even pre-‐installed) and start using its func-onali-es. Everyone can develop R packages, and everyone can share their R packages with others. The above is an extremely powerful concept and one of the key reasons R is so successful as a language and as a community. Namely, you don’t need to do all the hard core programming yourself or understand every complex detail of a par-cular algorithm or visualiza-on. You can simple use the out-‐of-‐the box func-ons that come with the relevant package as an interface to such func-onali-es. As such it is useful to have an understanding of R’s package ecosystem. Many R packages are available from the Comprehensive R Archive Network, and you can install them using the install.packages func-on. What is great about CRAN is that it associates packages with a par-cular task via Task Views. Alterna-vely, you can find R packages on bioconductor, github and bitbucket. Looking for a par-cular package and corresponding documenta-on? Try Rdocumenta-on, where you can easily search packages from CRAN, github and bioconductor.
When you download and install R, you would have auto downloaded and installed the base and selected packages.
www.utm.my innova-ve ● entrepreneurial ● global 9
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Example of Packages in R Every R package is simply a bundle of code that serves a specific purpose and is designed to be reusable by other developers. In addi-on to the primary codebase, packages o^en include data, documenta-on, and tests. As an R user, you can simply download a par-cular package (some are even pre-‐installed) and start using its func-onali-es. Everyone can develop R packages, and everyone can share their R packages with others. The above is an extremely powerful concept and one of the key reasons R is so successful as a language and as a community. Namely, you don’t need to do all the hard core programming yourself or understand every complex detail of a par-cular algorithm or visualiza-on. You can simple use the out-‐of-‐the box func-ons that come with the relevant package as an interface to such func-onali-es. As such it is useful to have an understanding of R’s package ecosystem. Many R packages are available from the Comprehensive R Archive Network, and you can install them using the install.packages func-on. What is great about CRAN is that it associates packages with a par-cular task via Task Views. Alterna-vely, you can find R packages on bioconductor, github and bitbucket. Looking for a par-cular package and corresponding documenta-on? Try Rdocumenta-on, where you can easily search packages from CRAN, github and bioconductor.
ggplot2: fancy plots . MICE: Takes care of your Missing Values . nortest: normality tests . AER: Applied Econometrics with R. hddtools: analyse hydrological data #March 2017
Packages can be found from: • Comprehensive R Archive Network (CRAN)
• bioconductor • Github
www.utm.my innova-ve ● entrepreneurial ● global 10
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Handy Tip
• How to check which packages are available locally on your R?
>library()
www.utm.my innova-ve ● entrepreneurial ● global 11
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
How to load up an R package Need to call up its library!!
>#library(name of package) >#example
>library(stats)
Package: stats
acf
acf2.AR
t.test
www.utm.my innova-ve ● entrepreneurial ● global 12
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
How to use a package already in your R Every R package is simply a bundle of code that serves a specific purpose and is designed to be reusable by other developers. In addi-on to the primary codebase, packages o^en include data, documenta-on, and tests. As an R user, you can simply download a par-cular package (some are even pre-‐installed) and start using its func-onali-es. Everyone can develop R packages, and everyone can share their R packages with others. The above is an extremely powerful concept and one of the key reasons R is so successful as a language and as a community. Namely, you don’t need to do all the hard core programming yourself or understand every complex detail of a par-cular algorithm or visualiza-on. You can simple use the out-‐of-‐the box func-ons that come with the relevant package as an interface to such func-onali-es. As such it is useful to have an understanding of R’s package ecosystem. Many R packages are available from the Comprehensive R Archive Network, and you can install them using the install.packages func-on. What is great about CRAN is that it associates packages with a par-cular task via Task Views. Alterna-vely, you can find R packages on bioconductor, github and bitbucket. Looking for a par-cular package and corresponding documenta-on? Try Rdocumenta-on, where you can easily search packages from CRAN, github and bioconductor.
Once installed, you need to load the package
>library(stats)
Choose desired packages #pop-‐up window. OK
Loading an R package >#library(name of package) >#example
www.utm.my innova-ve ● entrepreneurial ● global 13
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
How to use a package NOT available in your R Every R package is simply a bundle of code that serves a specific purpose and is designed to be reusable by other developers. In addi-on to the primary codebase, packages o^en include data, documenta-on, and tests. As an R user, you can simply download a par-cular package (some are even pre-‐installed) and start using its func-onali-es. Everyone can develop R packages, and everyone can share their R packages with others. The above is an extremely powerful concept and one of the key reasons R is so successful as a language and as a community. Namely, you don’t need to do all the hard core programming yourself or understand every complex detail of a par-cular algorithm or visualiza-on. You can simple use the out-‐of-‐the box func-ons that come with the relevant package as an interface to such func-onali-es. As such it is useful to have an understanding of R’s package ecosystem. Many R packages are available from the Comprehensive R Archive Network, and you can install them using the install.packages func-on. What is great about CRAN is that it associates packages with a par-cular task via Task Views. Alterna-vely, you can find R packages on bioconductor, github and bitbucket. Looking for a par-cular package and corresponding documenta-on? Try Rdocumenta-on, where you can easily search packages from CRAN, github and bioconductor.
● Suppose the package is not available in In order to use an R package, it must be installed locally and then load #requires internet
Once installed, you need to load the package as before
At the ribbon, select tab <Packages> Choose <Install package(s)> Choose any CRAN mirror #pop-‐up window Choose desired packages #pop-‐up window. OK
>library(name of package)
www.utm.my innova-ve ● entrepreneurial ● global 14
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Outline
PART B: Core of R 1. R func-ons (in-‐built) 2. R Packages 3. How to search for FuncNon/
Packages etc
www.utm.my innova-ve ● entrepreneurial ● global 15
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
How to search for funcNon/packages in R
Func-on/Packages?
R-‐help!
hmps://www.rdocumenta-on.org/
Google it!
www.utm.my innova-ve ● entrepreneurial ● global 16
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Use Help! in R
• Help: R func-on name KNOWN • Help: R func-on name NOT KNOWN
Help pages in R will tell you the source package of the func-on, describe the func-ons, give you the syntax argument, explana-on about the arguments and example. These func-ons can be nested and combined to make more complex expressions: > sqrt(sin(45*pi/180)) [1] 0.8408964
www.utm.my innova-ve ● entrepreneurial ● global 17
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Use Help! in R
There are many in-‐built func-ons in R, each with different individual arguments. We can seek help in R to learn more about these func-ons.
A) Help: R func-on name KNOWN > help(nameOffunction) > help(log) #help on function log > ?log #help on function log
or at the Ribbon select > Help >R func-ons (text)
Exercise: Obtain help on the R func-on “plot”
www.utm.my innova-ve ● entrepreneurial ● global 18
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Handy Tip
• If your help document fails to upload (url fails etc), type
> op-ons(help_type=“text”) #a text version of the help document will be displayed
18
www.utm.my innova-ve ● entrepreneurial ● global 19
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Help in R (R func-ons)
Use Help: R func-ons if you know the name of the func-ons used and need more info about it
Help: R funcNon name KNOWN
www.utm.my innova-ve ● entrepreneurial ● global 20
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Illustrate an R Help document
!!!!!!Output: Attach: see !help plot document!!
Exercise: Obtain help on the R func-on “plot”
www.utm.my innova-ve ● entrepreneurial ● global 21
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Func-on: plot Package: graphics
How to use the func-ons and what are its arguments
www.utm.my innova-ve ● entrepreneurial ● global 22
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
• Examples for the Rfunc-on • We can copy and paste the
codes on Rconsole • Recommended to run line by line
www.utm.my innova-ve ● entrepreneurial ● global 23
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Use Help! in R
There are many in-‐built func-ons in R, each with different individual arguments. We can seek help in R to learn more about these func-ons.
B) Help: R func-on name NOT KNOWN > help.start( ) or at the Ribbon select > Help >Search help... > help.search(“optimization”) > ?? Exercise: Find an R func-on that would calculate the inverse of a matrix
www.utm.my innova-ve ● entrepreneurial ● global 24
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Help in R (Search help)
• Use Search Help if you do not know the func-ons use in R
• Shows a list of related func-ons
• Then use help(“name of func-on”)
Help: R funcNon name NOT KNOWN
www.utm.my innova-ve ● entrepreneurial ● global 25
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Illustrate an R Help document
!!!!!!
Exercise: 1. Obtain help on sta-s-cal significance to find
difference between two popula-on means called Student t-‐test
2. Find an R func-on that would calculate the
inverse of a matrix
www.utm.my innova-ve ● entrepreneurial ● global 26
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Example of help search
stats: t.test
Name of Package
Name of func-on
www.utm.my innova-ve ● entrepreneurial ● global 27
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Once you have idenNfied the funcNon-‐ get help for the funcNon:
> ?t.test!
www.utm.my innova-ve ● entrepreneurial ● global 28
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
IMPORTANT:
t.test{stats}
In order to use any R func-on (even the help documenta-on for the func-on), you need to make sure that (1) the package has already been installed locally in your computer (2) The library (or door) to the package needs to be opened
The loca-on where the packages are stored in your computer a^er it is downloaded is called the library.
If you can’t open your library, this implies that the package is not in your R. Thus, you need to install the package first.!
www.utm.my innova-ve ● entrepreneurial ● global 29
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
Once you have installed the package and open its library, -‐ you can use / get help for the funcNon:
> ?t.test!
www.utm.my innova-ve ● entrepreneurial ● global 30
Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad
NEXT
● PART C: Data Object Structure in R