when row value matches in 2 columns of data, replace the value in the second column with the value in the first column in r

You can achieve this by using the dplyr package in R. Here's the code to accomplish the task:

library(dplyr)

# Create a sample data frame
df <- data.frame(col1 = c("A", "B", "C", "D"),
                 col2 = c("E", "B", "F", "A"))

# Update col2 where col1 and col2 match
df <- df %>%
  mutate(col2 = ifelse(col1 == col2, col1, col2))

df
247 chars
12 lines

This code will replace the values in col2 with the corresponding values in col1 if they match.

related categories

gistlibby LogSnag