Explain about initialize() function in R?

This recipe explains what about initialize() function in R

Recipe Objective

When we declare an object in R, the object can be classified either as public data element or private data element. Initialise is a function used to initialise a variable in the form of private data element ​

This recipe demonstrates how to initilise any values to the objects at the time of declaration of the objects. ​

STEP 1: Creating a user defined class

We do that by inclusisng public and private data members ​

library(R6) football <- R6Class( "Football", private = list( name_of_player = NA, goals_scored = NA), public = list( initialize = function(x,y){ private$name_of_player <-x private$goals_scored <-y }) )

The above code means that we trying to initialise the values of "name_of_player" and "goals_scored" during the time of execution/declaration ​

STEP 2: Calling the function and initialising the values to objects

man_utd <- football$new("Paul Pogba", 11) man_utd

  Public:
    clone: function (deep = FALSE) 
    initialize: function (x, y) 
  Private:
    goals_scored: 11
    name_of_player: Paul Pogba

This means that we have succeeded in initialzing the values "Paul Pogba" to name_of_player and "11" to goals_scored. ​

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Microsoft Fabric Project to Build a Financial Reporting Agent
In this Microsoft Fabric project, you'll build a financial reporting agent that simplifies data management, automates analysis, and delivers real-time dashboards for wealth advisors and their clients.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

OSZAR »