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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

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.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

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

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

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 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 Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python