Replace Values Based on Condition in R
Last Updated :
26 Mar, 2024
In this article, we will examine various methods to replace values based on conditions in the R Programming Language.
How to replace values based on condition
R language offers a method to replace values based on conditions efficiently. By using these methods provided by R, it is possible to replace values based on condition easily. Some of the methods to replace values based on condition are:
Method 1: Replacing value based on a Single condition
This method is used to replace value or data based on a single condition efficiently. In the below example, we created the data frame and replaced the value based on a single Condition.
R
# Creating data frame
df <- data.frame(
name=c("A", "B", "C", "D", "E"),
age=c(24,21,23,25,24),
id_no=c(4203,4438,4477,4486,4576))
print("The data frame is")
print(df)
df$age[df$age == 24] = 55
print("Replacing value based on single condition")
print(df)
Output:
[1] "The data frame is"
name age id_no
1 A 24 4203
2 B 21 4438
3 C 23 4477
4 D 25 4486
5 E 24 4576
[1] "Replacing value based on single condition"
name age id_no
1 A 55 4203
2 B 21 4438
3 C 23 4477
4 D 25 4486
5 E 55 4576
In the below example, we created the data frame and replaced the value based on single Condition.
R
# Creating data frame
df <- data.frame(
A=c(10:14),
B=c("a","b","c","d","a"),
C=c("abc","def","mno","pqr","abc"))
print("The data frame is")
print(df)
df$C[df$C== "abc"] = "gfg"
print("Replacing value based on single condition")
print(df)
Output:
[1] "The data frame is"
A B C
1 10 a abc
2 11 b def
3 12 c mno
4 13 d pqr
5 14 a abc
[1] "Replacing value based on single condition"
A B C
1 10 a gfg
2 11 b def
3 12 c mno
4 13 d pqr
5 14 a gfg
Method 2: Replacing value based on Multiple Conditions
This method is used to replace value or data based on a multiple Conditions efficiently. In the below example, we created the data frame and replaced the value based on multiple Conditions .
R
nos=c(50,20,30,40,50)
r_no=c(105,102,103,104,105)
emp_id=c(2302:2306)
# Creating data frame
df <- data.frame(nos, r_no, emp_id)
print("The data frame is")
print(df)
df$nos[df$nos == 50 & df$r_no == 105] <- 600
print("Replacing value based on multiple conditions")
print(df)
Output:
[1] "The data frame is"
nos r_no emp_id
1 50 105 2302
2 20 102 2303
3 30 103 2304
4 40 104 2305
5 50 105 2306
[1] "Replacing value based on multiple conditions"
nos r_no emp_id
1 600 105 2302
2 20 102 2303
3 30 103 2304
4 40 104 2305
5 600 105 2306
In the below example, we created the data frame and replaced the value based on multiple Conditions.
R
# Creating data frame
df = data.frame(
name=c("ramya", "kumari" ,"raghu", "sravya", "kumari"),
sex=c("F", "F", "M", "F","F"),
state=c("Andhra pradesh","odhisa","delhi","kerla","odhisa"))
print("The data frame is")
print(df)
df$state[df$state == "odhisa" & df$name =="kumari" ] = "Hyderabad"
print("Replacing value based on multiple conditions")
print(df)
Output:
[1] "The data frame is"
name sex state
1 ramya F Andhra pradesh
2 kumari F odhisa
3 raghu M delhi
4 sravya F kerla
5 kumari F odhisa
[1] "Replacing value based on multiple conditions"
name sex state
1 ramya F Andhra pradesh
2 kumari F Hyderabad
3 raghu M delhi
4 sravya F kerla
5 kumari F Hyderabad
Conclusion
In conclusion, we learned about how to replace values based on condition by using R. R language offers versatile tools while performing replacing the data efficiently.
Similar Reads
Replace Missing Values by Column Mean in R DataFrame In this article, we are going to see how to replace missing values with columns mean in R Programming Language. Missing values in a dataset are usually represented as NaN or NA. Such values must be replaced with another value or removed. This process of replacing another value in place of missing da
4 min read
How to Remove rows based on count of a specific value in R? Data cleaning is an essential step in data analysis, and removing rows based on specific criteria is a common task. One such criterion is the count of a specific value in a column. This article will guide you through the process of removing rows from a data frame in R based on the count of a specifi
5 min read
Replace values of a Factor in R Programming - recode_factor() Function Factors in R programming are kind of data structures that stores categorical data i.e., levels and can have any type of data (integer, string, etc). recode_factor() function in R Language is used to replace certain values in a factor. To use recode_factor() function, dplyr package is required. Synta
1 min read
Take random sample based on groups in R R programming language provides us with many packages to take random samples from data objects, data frames, or data tables and aggregate them into groups. Method 1: Using plyr library The "plyr" library can be installed and loaded into the working space which is used to perform data manipulation an
4 min read
Filter Rows Based on Conditions in a DataFrame in R In this article, we will explore various methods to filter rows based on Conditions in a data frame by using the R Programming Language. How to filter rows based on Conditions in a data frame R language offers various methods to filter rows based on Conditions in a data frame. By using these methods
3 min read
How to Replace particular value in R dataframe ? Often, some values in our dataframe are not appropriate, they are not up-to-date, or we aren't aware of those values. In such cases, we replace those values, because they are causing ambiguity. Over here, we will use the term NA, which stands for Non-Available to replace the unknown values. In this
4 min read