Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Best R Packages for Machine Learning
Next article icon

Best R Packages for Machine Learning

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Machine Learning is a subset of artificial intelligence that focuses on the development of computer software or programs that access data to learn from them and make predictions.

7-Best-R-Packages-for-Machine-Learning-1

R language is being used in building machine learning models due to its flexibility, efficient packages and the ability to perform deep learning models with integration to the cloud. Being an open-source language, it offers multiple packages. Following are some famous R packages widely used in industry.

1. data.table

data.table package is a enhanced version of data.frame package and is designed for high-performance. It is known for its memory efficiency and ability to perform complex data manipulations at high speed. Some key features of data.table are:

  • Fast file reading and writing
  • Scalable data aggregation with parallelism support
  • Feature-rich data reshaping
  • Simplified syntax for subsetting and merging data
R
install.packages("data.table") library(data.table)  iris_dt <- as.data.table(iris)  result <- iris_dt[Species == "setosa" & Sepal.Length > 5][1:5] result 

Output:

data_table
Data Table

2. Dplyr

Dplyr package is one of the most widely used data manipulation tools in R. It provides easy to implement and consistent set of functions to perform data transformations. The key functions in dplyr are:

  • select(): Choose columns by name
  • filter(): Subset rows based on conditions
  • arrange(): Sort rows by column values
  • mutate(): Add new variables

Select and Mutate Functions :

R
install.packages("dplyr")  # Run only once library(dplyr)  data("mtcars")  cat("---- Select ----\n") selected <- dplyr::select(mtcars, mpg, cyl) head(selected) cat("\n------------------\n")  cat("---- Mutate ----\n") mutated <- dplyr::mutate(mtcars, power_to_weight = hp / wt) head(mutated) cat("\n------------------\n") 

Output:

select-and-filter
Select and Mutate

Filter and Arrange Functions :

Python
cat("---- Filter ----\n") filtered <- dplyr::filter(mtcars, cyl == 6) head(filtered) cat("\n------------------\n")  cat("---- Arrange ----\n") arranged <- dplyr::arrange(mtcars, desc(mpg)) head(arranged) cat("\n------------------\n") 

Output:

Filter-
Filter and Arrange

3. ggplot2

ggplot2 is an open-source visualization package based on the Grammar of Graphics. It is widely regarded as one of the most famous and flexible visualization libraries in R. With ggplot2 users can create a wide range of static and interactive visualizations including:

  • Bar charts
  • Scatter plots
  • Line graphs
  • Histograms
  • Boxplots

The syntax is easy and visualizations are highly customizable making it go-to package for data visualization in R.

R
install.packages("dplyr")  install.packages("ggplot2") library(dplyr)  library(ggplot2)  ggplot(data = mtcars,          aes(x = hp, y = mpg,             col = disp)) + geom_point()  

Output:

Output

4. caret

caret package (Classification and Regression Training) provides a comprehensive framework for building machine learning models in R. It includes tools for:

  • Data splitting
  • Preprocessing
  • Feature selection
  • Model training
  • Model evaluation

caret supports numerous machine learning algorithms and is commonly used in industry due to its ease of use and flexibility.

R
install.packages("e1071") install.packages("caTools") install.packages("caret")  library(e1071) library(caTools) library(caret)  data(iris)  split <- sample.split(iris, SplitRatio = 0.7) train_cl <- subset(iris, split == TRUE) test_cl <- subset(iris, split == FALSE)  train_scale <- scale(train_cl[, 1:4]) test_scale <- scale(test_cl[, 1:4])  set.seed(120) classifier_cl <- naiveBayes(Species ~ ., data = train_cl)  y_pred <- predict(classifier_cl, newdata = test_cl)  cm <- table(test_cl$Species, y_pred) print(classifier_cl) print(cm) 

Output:

  • Model classifier_cl:
nb
Navie Bayers Model
  • Confusion Matrix
caret_cm
caret_cm

5. e1071

e1071 package is known for its implementation of various machine learning algorithms including support vector machines (SVM), clustering algorithms and K-Nearest Neighbors (KNN). It is widely used for classification, regression and clustering tasks.

R
install.packages("e1071") install.packages("caTools") install.packages("class")  library(e1071) library(caTools) library(class)  data(iris)  split <- sample.split(iris, SplitRatio = 0.7) train_cl <- subset(iris, split == TRUE) test_cl <- subset(iris, split == FALSE)  train_scale <- scale(train_cl[, 1:4]) test_scale <- scale(test_cl[, 1:4])  classifier_knn <- knn(train = train_scale,                       test = test_scale,                       cl = train_cl$Species,                       k = 1)  cm <- table(test_cl$Species, classifier_knn) print(cm)  misClassError <- mean(classifier_knn != test_cl$Species) print(paste('Accuracy =', 1 - misClassError)) 

Outputs: 

KNN
KNN

6. XGBoost

XGBoost is a implementation of gradient boosting algorithms and is useful for large datasets. It is widely used in machine learning due to its performance and scalability. XGBoost works by bagging and boosting techniques to improve model accuracy.

R
install.packages("data.table")  install.packages("dplyr")  install.packages("ggplot2")  install.packages("caret")  install.packages("xgboost")  install.packages("e1071")  install.packages("cowplot")   library(data.table)  library(dplyr)  library(ggplot2)  library(caret)  library(xgboost)  library(e1071)  library(cowplot)   test[, Item_Outlet_Sales := NA]   combi = rbind(train, test)   missing_index = which(is.na(combi$Item_Weight))  for(i in missing_index){    item = combi$Item_Identifier[i]    combi$Item_Weight[i] = mean(combi$Item_Weight[combi$Item_Identifier == item], na.rm = T)  }   zero_index = which(combi$Item_Visibility == 0)  for(i in zero_index){    item = combi$Item_Identifier[i]    combi$Item_Visibility[i] = mean(combi$Item_Visibility[combi$Item_Identifier == item], na.rm = T)  }   combi[, Outlet_Size_num := ifelse(Outlet_Size == "Small", 0, ifelse(Outlet_Size == "Medium", 1, 2))]  combi[, Outlet_Location_Type_num := ifelse(Outlet_Location_Type == "Tier 3", 0, ifelse(Outlet_Location_Type == "Tier 2", 1, 2))]  combi[, c("Outlet_Size", "Outlet_Location_Type") := NULL]   ohe_1 = dummyVars("~.", data = combi[, -c("Item_Identifier", "Outlet_Establishment_Year", "Item_Type")], fullRank = T)  ohe_df = data.table(predict(ohe_1, combi[, -c("Item_Identifier", "Outlet_Establishment_Year", "Item_Type")]))  combi = cbind(combi[, "Item_Identifier"], ohe_df)   skewness(combi$Item_Visibility)   skewness(combi$price_per_unit_wt)   combi[, Item_Visibility := log(Item_Visibility + 1)]    num_vars = which(sapply(combi, is.numeric))   num_vars_names = names(num_vars)  combi_numeric = combi[, setdiff(num_vars_names, "Item_Outlet_Sales"), with = F]  prep_num = preProcess(combi_numeric, method = c("center", "scale"))  combi_numeric_norm = predict(prep_num, combi_numeric)  combi[, setdiff(num_vars_names, "Item_Outlet_Sales") := NULL]   combi = cbind(combi, combi_numeric_norm)   train = combi[1:nrow(train)]  test = combi[(nrow(train) + 1):nrow(combi)]  test[, Item_Outlet_Sales := NULL]    param_list = list(    objective = "reg:linear",    eta = 0.01,    gamma = 1,    max_depth = 6,    subsample = 0.8,    colsample_bytree = 0.5  )   Dtrain = xgb.DMatrix(data = as.matrix(train[, -c("Item_Identifier", "Item_Outlet_Sales")]), label = train$Item_Outlet_Sales)  Dtest = xgb.DMatrix(data = as.matrix(test[, -c("Item_Identifier")]))   set.seed(112)  xgbcv = xgb.cv(params = param_list, data = Dtrain, nrounds = 1000, nfold = 5, print_every_n = 10, early_stopping_rounds = 30, maximize = F)   xgb_model = xgb.train(data = Dtrain, params = param_list, nrounds = 428)  xgb_model 

Output:

xgb
XGBoost Model

7. randomForest

Random Forest in R Programming is an ensemble learning method that builds multiple decision trees and combines them to provide more accurate predictions. It is especially useful for classification and regression tasks. Each decision tree is trained on a subset of the data and predictions are made by aggregating the results of all trees.

R
install.packages("caTools") install.packages("randomForest")  library(caTools)  library(randomForest)   data(iris)   split <- sample.split(iris, SplitRatio = 0.7)  train <- subset(iris, split == "TRUE")  test <- subset(iris, split == "FALSE")   set.seed(120)  classifier_RF = randomForest(x = train[-5], y = train$Species, ntree = 500)   classifier_RF   y_pred = predict(classifier_RF, newdata = test[-5])  cm = table(test[, 5], y_pred)  cm 

Outputs:

  • Model classifier_RF:
Screenshot-2025-04-16-164956
Random Forest
  • Confusion Matrix:
rfcom
Confusion Matrix

R provides many packages that are widely used for data manipulation, visualization and machine learning. These packages discussed in this article are just few packages available in R for data scientists and researchers.


Next Article
Best R Packages for Machine Learning

D

dhruv5819
Improve
Article Tags :
  • GBlog
  • Machine Learning
  • R Language
  • AI-ML-DS Blogs
  • AI-ML-DS
  • R Machine-Learning
Practice Tags :
  • Machine Learning

Similar Reads

    Machine Learning Tutorial
    Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Machin
    5 min read

    Prerequisites for Machine Learning

    Python for Machine Learning
    Welcome to "Python for Machine Learning," a comprehensive guide to mastering one of the most powerful tools in the data science toolkit. Python is widely recognized for its simplicity, versatility, and extensive ecosystem of libraries, making it the go-to programming language for machine learning. I
    6 min read
    SQL for Machine Learning
    Integrating SQL with machine learning can provide a powerful framework for managing and analyzing data, especially in scenarios where large datasets are involved. By combining the structured querying capabilities of SQL with the analytical and predictive capabilities of machine learning algorithms,
    6 min read

    Getting Started with Machine Learning

    Advantages and Disadvantages of Machine Learning
    Machine learning (ML) has revolutionized industries, reshaped decision-making processes, and transformed how we interact with technology. As a subset of artificial intelligence ML enables systems to learn from data, identify patterns, and make decisions with minimal human intervention. While its pot
    3 min read
    Why ML is Important ?
    Machine learning (ML) has become a cornerstone of modern technology, revolutionizing industries and reshaping the way we interact with the world. As a subset of artificial intelligence (AI), ML enables systems to learn and improve from experience without being explicitly programmed. Its importance s
    4 min read
    Real- Life Examples of Machine Learning
    Machine learning plays an important role in real life, as it provides us with countless possibilities and solutions to problems. It is used in various fields, such as health care, financial services, regulation, and more. Importance of Machine Learning in Real-Life ScenariosThe importance of machine
    13 min read
    What is the Role of Machine Learning in Data Science
    In today's world, the collaboration between machine learning and data science plays an important role in maximizing the potential of large datasets. Despite the complexity, these concepts are integral in unraveling insights from vast data pools. Let's delve into the role of machine learning in data
    9 min read
    Top Machine Learning Careers/Jobs
    Machine Learning (ML) is one of the fastest-growing fields in technology, driving innovations across healthcare, finance, e-commerce, and more. As companies increasingly adopt AI-based solutions, the demand for skilled ML professionals is Soaring. Machine Learning JobsThis article delves into the Ty
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences