Replace Values Based on Condition in R Last Updated : 21 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In R programming language, we can change (replace) certain values in a dataset only when a condition is true. This is called replacing values based on a condition.Method 1: Replacing value based on a Single conditionWe replace values in a column when a single condition is true.Example 1: We will replace values in the age column where age == 24.df: The data frame with columns name, age and id_no.df$age == 24: Condition - selects rows where age is 24.df$age[...] <- 55: Replaces those values with 55. R 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:OutputExample 2: We will replace values in the C column where C == "abc".df: The data frame with columns A, B and C.df$C == "abc": Condition - selects rows where C is "abc".df$C[...] <- "gfg": Replaces those values with "gfg". R df <- data.frame( A = 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:OutputMethod 2: Replacing value based on Multiple ConditionsWe replace values in a column only when two or more conditions are true.Example 1: We will replace values in the nos column where nos == 50 and r_no == 105.df: The data frame with columns nos, r_no and emp_id.df$nos == 50: First condition – selects rows where nos is 50.df$r_no == 105: Second condition – selects rows where r_no is 105.&: Combines both conditions (AND condition).df$nos[...] <- 600: Replaces matching nos values with 600. R nos <- c(50, 20, 30, 40, 50) r_no <- c(105, 102, 103, 104, 105) emp_id <- c(2302:2306) 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:OutputExample 2: We will replace values in the state column where state == "odhisa" and name == "kumari".df: The data frame with columns name, sex and state.df$state == "odhisa": First condition - selects rows where state is "odhisa".df$name == "kumari": Second condition - selects rows where name is "kumari".&: Combines both conditions.df$state[...] <- "Hyderabad": Replaces matched values in state with "Hyderabad". R 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:OutputThe output shows that the value "odhisa" is replaced with "Hyderabad" only for rows where the name is "kumari". All other rows remain unchanged. Comment More infoAdvertise with us Next Article How to Replace particular value in R dataframe ? S sumapriyfs0e Follow Improve Article Tags : R Language R DataFrame-Function R Data-science AI-ML-DS With R R Language +1 More 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 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 Fastest Way to Replace NAs in a Large data.table in R Handling missing values is a crucial step in data preprocessing. In R the data. table package offers efficient ways to manipulate large datasets including the task of replacing NAs. This article will guide us through the fastest methods to replace NAs in large data. table ensuring the optimal perfor 4 min read Like