Book Image

Machine Learning with R Cookbook

By : Yu-Wei, Chiu (David Chiu)
Book Image

Machine Learning with R Cookbook

By: Yu-Wei, Chiu (David Chiu)

Overview of this book

<p>The R language is a powerful open source functional programming language. At its core, R is a statistical programming language that provides impressive tools to analyze data and create high-level graphics.</p> <p>This book covers the basics of R by setting up a user-friendly programming environment and performing data ETL in R. Data exploration examples are provided that demonstrate how powerful data visualization and machine learning is in discovering hidden relationships. You will then dive into important machine learning topics, including data classification, regression, clustering, association rule mining, and dimension reduction.</p>
Table of Contents (21 chapters)
Machine Learning with R Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Resources for R and Machine Learning
Dataset – Survival of Passengers on the Titanic
Index

Downloading and installing R


To use R, you must first install it on your computer. This recipe gives detailed instructions on how to download and install R.

Getting ready

If you are new to the R language, you can find a detailed introduction, language history, and functionality on the official website (http://www.r-project.org/). When you are ready to download and install R, please access the following link: http://cran.r-project.org/.

How to do it...

Please perform the following steps to download and install R for Windows and Mac users:

  1. Go to the R CRAN website, http://www.r-project.org/, and click on the download R link, that is, http://cran.r-project.org/mirrors.html):

  2. You may select the mirror location closest to you:

    CRAN mirrors

  3. Select the correct download link based on your operating system:

    Click on the download link based on your OS

As the installation of R differs for Windows and Mac, the steps required to install R for each OS are provided here.

For Windows users:

  1. Click on Download R for Windows, as shown in the following screenshot, and then click on base:

    Go to "Download R for Windows" and click "base"

  2. Click on Download R 3.x.x for Windows:

    Click "Download R 3.x.x for Windows"

  3. The installation file should be downloaded. Once the download is finished, you can double-click on the installation file and begin installing R:

  4. The Windows installation of R is quite straightforward; the installation GUI may instruct you on how to install the program step by step (public license, destination location, select components, startup options, startup menu folder, and select additional tasks). Leave all the installation options as the default settings if you do not want to make any changes.

  5. After successfully completing the installation, a shortcut to the R application will appear in your Start menu, which will open the R Console:

    The Windows R Console

For Mac OS X users:

  1. Go to Download R for (Mac) OS X, as shown in this screenshot.

  2. Click on the latest version (.pkg file extension) according to your Mac OS version:

  3. Double-click on the downloaded installation file (.pkg extension) and begin to install R. Leave all the installation options as the default settings if you do not want to make any changes:

  4. Follow the onscreen instructions, Introduction, Read Me, License, Destination Select, Installation Type, Installation, Summary, and click on continue to complete the installation.

  5. After the file is installed, you can use Spotlight Search or go to the application folder to find R:

    Use "Spotlight Search" to find R

  6. Click on R to open R Console:

As an alternative to downloading a Mac .pkg file to install R, Mac users can also install R using Homebrew:

  1. Download XQuartz-2.X.X.dmg from https://xquartz.macosforge.org/landing/.

  2. Double-click on the .dmg file to mount it.

  3. Update brew with the following command line:

    $ brew update
    
  4. Clone the repository and symlink all its formulae to homebrew/science:

    $ brew tap homebrew/science
    
  5. Install gfortran:

    $ brew install gfortran
    
  6. Install R:

    $ brew install R
    

For Linux users, there are precompiled binaries for Debian, Red Hat, SUSE, and Ubuntu. Alternatively, you can install R from a source code. Besides downloading precompiled binaries, you can install R for Linux through a package manager. Here are the installation steps for CentOS and Ubuntu.

Downloading and installing R on Ubuntu:

  1. Add the entry to the /etc/apt/sources.list file:

    $ sudo sh -c "echo 'deb http:// cran.stat.ucla.edu/bin/linux/ubuntu precise/' >> /etc/apt/sources.list"
    
  2. Then, update the repository:

    $ sudo apt-get update
    
  3. Install R with the following command:

    $ sudo apt-get install r-base
    
  4. Start R in the command line:

    $ R
    

Downloading and installing R on CentOS 5:

  1. Get rpm CentOS5 RHEL EPEL repository of CentOS5:

    $ wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
    
  2. Install CentOS5 RHEL EPEL repository:

    $ sudo rpm -Uvh epel-release-5-4.noarch.rpm
    
  3. Update the installed packages:

    $ sudo yum update
    
  4. Install R through the repository:

    $ sudo yum install R
    
  5. Start R in the command line:

    $ R
    

Downloading and installing R on CentOS 6:

  1. Get rpm CentOS5 RHEL EPEL repository of CentOS6:

    $ wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    
  2. Install the CentOS5 RHEL EPEL repository:

    $ sudo rpm -Uvh epel-release-6-8.noarch.rpm
    
  3. Update the installed packages:

    $ sudo yum update
    
  4. Install R through the repository:

    $ sudo yum install R
    
  5. Start R in the command line:

    $ R
    

How it works...

CRAN provides precompiled binaries for Linux, Mac OS X, and Windows. For Mac and Windows users, the installation procedures are straightforward. You can generally follow onscreen instructions to complete the installation. For Linux users, you can use the package manager provided for each platform to install R or build R from the source code.

See also