3 R and
3 R and
(Part 1)
Contents
3
History of R
http://www.r-project.org/
http://cran.r-project.org/doc/contrib/Verzani-SimpleR.pdf
Download R and RStudio
Download R :
http://cran.r-project.org/bin/
Download RStudio :
http://www.rstudio.com/ide/download/desktop
Installation
Installing R on windows PC :
Installing R on Linux:
sudo apt-get install r-base-core
Installation
Installing RStudio:
Click on the version recommended for your system, or the latest Windows version,
and save the executable file. Run the .exe file and follow the installation
instructions.
Version
Get R version
R.Version()
• You can type your own program at the prompt line >.
Getting help from R console
◦ help.start()
◦ help(topic)
◦ ?topic
◦ ??topic
Packages
R consists of a core and packages.
Packages contain functions that are not available in the core.
R packages are the collection of R functions, sample data, and compile
codes.
In the R environment, these packages are stored under a directory called
"library."
During installation, R installs a set of packages. We can add packages later
when they are needed for some specific purpose. Only the default
packages will be available when we start the R console.
One of the strengths of R is that the system can easily be extended. The
system allows you to write new functions and package those functions in a
so called `R package' (or `R library').
Packages
◦ When you download R, already a number (around 30) of
packages are downloaded as well.
◦ To use a function in an R package, that package has to be
attached to the system.
◦ When you start R not all of the downloaded packages are
attached, nearly seven packages are attached to the system
by default.
◦ You can use the function search to see a list of packages that
are currently attached to the system, this list is also called the
search path.
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:datasets" "package:utils"
[7] "package:methods" "Autoloads" "package:base"
13
Packages
◦ The function library() can also be used to list all the available libraries on
your system with a short description. Run the function without any
arguments
> library()
Packages in library 'C:/PROGRA~1/R/R-25~1.0/library':
base The R Base Package
Boot Bootstrap R (S-Plus) Functions (Canty)
class Functions for Classification
cluster Cluster Analysis Extended Rousseeuw et al.
codetools Code Analysis Tools for R
datasets The R Datasets Package
DBI R Database Interface
foreign Read Data Stored by Minitab, S, SAS, SPSS,
Stata, Systat, dBase, ...
graphics The R Graphics Package
14
Packages
To attach another package to the system you can use the menu or
the library function. Via the menu:
Select the `Packages' menu and select `Load package...', a list of available
packages on your system will be displayed. Select one and click `OK',
the package is now attached to your current R session. Via the library
function:
> library(MASS)
15
Packages
Get library locations containing R
.libPaths()
Syntax:
install.packages(file_name_with_path, repos = NULL, type = "source")
Example:
# Install the package named "XML"
install.packages("E:/XML_3.98-1.3.zip", repos = NULL, type = "source")
Introduction to swirl
Swirl teaches you R programming and data science interactively, at your
own pace, and right in the R console!
Install swirl
Open RStudio (or just plain R if you don't have RStudio) and type the following
into the console:
install.packages("swirl")
Start swirl using following commands on R console:
library("swirl")
swirl()
Introduction to swirl
>swirl()
| Welcome to swirl!
| Please sign in. If you've been here before, use the same name as you did then. If you are new, call yourself
something unique. What shall I call you? Johnny
| Please choose a course, or type 0 to exit swirl.
1: R Programming
2: Take me to the swirl course repository!
Selection: 1
| Please choose a lesson, or type 0 to return to course menu.
1: Basic Building Blocks 11: vapply and tapply
2: Workspace and Files 12: Looking at Data
3: Sequences of Numbers 13: Simulation
4: Vectors 14: Dates and Times
5: Missing Values 15: Base Graphics
6: Subsetting Vectors
7: Matrices and Data Frames
8: Logic
9: Functions
10: lapply and sapply