'Using R - If columns A and B do not match, replace entries in columns C and D with 'NA'

I have 4 columns in a dataframe:

dd <- data.frame(
  A= c(102,101,100,107,55),
  B= c(102,101,30,77,55),
  C= c("cycle", "walk", "walk", "walk", "drive"),
  D= c("English", "French", "English", "English", "Japanese")
)

Using R, I wish to compare if entries in columns A and B matches. If entries in A and B do not match, then I wish to replace their corresponding entries in column C and D with 'NA'.

desired <- data.frame(
  A= c(102,101,100,107, 55),
  B= c(102,101,30,77, 55),
  C= c("cycle", "walk", NA, NA, "drive"),
  D= c("English", "French", NA, NA, "Japanese")
)

Can anyone advise? The qn is not new but the proposed solutions are usually in Excel. I hope to do this in R. Thank you in advance!

r


Solution 1:[1]

A simple option

df[df$A!=df$B,c("C","D")]=NA

if A and B are factors you could try

df[as.character(df$A)!=as.character(df$B),c("C","D")]=NA

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1