How to replace items in dataframe using a condition in R?

This recipe helps you replace items in dataframe using a condition in R

Recipe Objective

In data analysis, you will have to deal with missing values, negative values that are present in the dataset. These values needs to be replaced with appropriate values and to do so we will use replace() function.

This recipe demonstrates how to replace values in dataframe using conditions in replace() function. ​

Explore Interesting IoT Project Ideas for Practice

STEP 1: Load necessary package

# for data manipulation library(tidyverse)

STEP 2: Read a csv file and explore the data

data <- read.csv("R_368_entry.csv") glimpse(data)

Rows: 19
Columns: 3
$ ï..count      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,...
$ observation1  -30, -120, 50, 30, 10, -10, 230, 220, 210, 200, 190, 1...
$ observation2  -110, 40, 160, -100, 50, 40, 30, 20, 10, 0, 80, -90, 7...

summary(data) # returns the statistical summary of the data columns

    ï..count     observation1     observation2    
 Min.   : 1.0   Min.   :-120.0   Min.   :-110.00  
 1st Qu.: 5.5   1st Qu.:  40.0   1st Qu.:  15.00  
 Median :10.0   Median : 140.0   Median :  40.00  
 Mean   :10.0   Mean   : 112.6   Mean   :  33.16  
 3rd Qu.:14.5   3rd Qu.: 185.0   3rd Qu.:  65.00  
 Max.   :19.0   Max.   : 230.0   Max.   : 160.00  

dim(data)

19 3

STEP 3: Using replace() function

We will replace the negative values with zero 0

Syntax: replace(x, list, values)

where:

  1. x = vectorthat contains the negative values
  2. boolean_array = condition which will be used to replace the values
  3. values = values with which the vector is replaced

replaced_entry2 = replace(data$observation2, data$observation2 < 0, 0 ) print(replaced_entry2)

[1]   0  40 160   0  50  40  30  20  10   0  80   0  70  60  50  40  30 100 150

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

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.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

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