How to create an object in R

In this recipe, we will learn what are objects in R and how to create them with the help of examples.

How to create objects in R?

In this tutorial, you will learn –
• What are R objects?
• How to create an object in R?

What are R objects?

Objects in R, are what we call variables in other programming languages. They are instances of a class. A vector, a matrix, a dataframe, a list, an array, or a factor are all objects in R

Access Avocado Machine Learning Project for Price Prediction 

How to create an object in R?

You can create an object using the “<-“ or an equals “=” sign. Both of these are used to assign values to objects. There are a few rules to keep in mind while naming an object in R. They are –

     Object names should be short and explicit.
     Object names cannot start with a number.
     “Data” is different than “data” as R is case sensitive.
     The names of default fundamental functions should not be used as object names in R. eg: function, if, else, repeat, etc.

Let us check out an example. We are creating an object distance_km that stores the distance covered in kilometers.

Code:
#assigning value to an object
distance_km <- 134

#printing value stored in the object
distance_km

Output:
[1] 134

Once you have created an object, you can perform several arithmetic operations on it.

Code: #creating another object
time_hr <- 1.5

#arithmetic operation
speed <- distance_km / time_hr

#printing the value stored in object speed
speed

Output:
[1] 89.33333

You may also want to check out the following recipe –

How to find out the size of an object in R
https://www.projectpro.io/recipes/find-size-of-object-r

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

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

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 .

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP