Open In App

Replace Values Based on Condition in R

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads