Open In App

Setting up Environment for Machine Learning with R Programming

Last Updated : 25 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Machine Learning is a subset of Artificial Intelligence (AI) which enables systems to learn and make predictions without explicit programming. In machine learning, algorithms and models are developed to identify patterns and trends within data, allowing systems to predict outcomes based on observed data. These models learn from the data and store rules that can be applied to new or unseen datasets for prediction of data.

Setting up an environment for Machine Learning using Anaconda

Step 1: Install Anaconda (Linux, Windows) and launch the navigator.

Step 2: Open Anaconda Navigator and click the Install button for RStudio.

anaconda-navigator

Step 3: After installation, create a new environment. Anaconda will then send a prompt asking to enter a name for the new environment and the lunch the R studio.

create-new-environment

Running R commands

Method 1: R commands can run from the console provided in R studio. After opening Rstudio simply type R commands to the console.

running-R-command

Method 2: R commands can be stored in a file and can be executed in an anaconda prompt. This can be achieved by the following steps.

  1. Open an anaconda prompt
  2. Go to the directory where the R file is located
  3. Activate the anaconda environment by using the command:

    conda activate <ENVIRONMENT_NAME>

  4. Run the file by using the command:

    Rscript <FILE_NAME>.R

running-R-command-in-anaconda-console

Installing Machine Learning Packages in R

Packages contain a set of predefined functions that perform various tasks. The most used machine learning packages are Caret, e1071, net, kernlab and randomforest. There are two methods that can be used to install these packages for your R program.

Method 1: Installing Packages through Rstudio

  1. Open Rstudio and click the Install Packages option under Tools which is present in the menubar. install-packages
  2. Enter the names of all the packages you want to install separated by spaces or commas and then click install. install-packages

Method 2: Installing Packages through Anaconda Prompt or Rstudio console

  1. Open an Anaconda prompt.
  2. Switch the environment to the environment you used for Rstudio using the command:

    conda activate <ENVIRONMENT_NAME>

  3. Enter the command r to open the R console.
  4. Install the required packages using the command:

    install.packages(c("<PACKAGE_1>", "<PACKAGE_2>", ..., "<PACKAGE_N>"))

  5. installing-R-packagesWhile downloading the packages you might be prompted to choose a CRAN mirror. It is recommended to choose the location closest to you for a faster download.

ML with R


Next Article

Similar Reads