Home > Enterprise >  Replace value in multiple columns of dataframe based on ID
Replace value in multiple columns of dataframe based on ID

Time:11-24

I have a dataframe similar to the below

ID FirstName LastName Orders Age
1 John Smith 3 30
2 John Smith 7 8
3 John Smith 9 317
4 John Smith 12 20

I need to do the equivalent of a find and replace based on a number of IDs where if a match is found within the list, 'FirstName', 'LastName' and 'Age' are changed to value 'Redacted'. So I can start with setting up IDList like below with the necessary IDs:

IDList <- c(1,3,74,312)

But can anyone please advise how to code in order that final table would look like this:

ID FirstName LastName Orders Age
1 Redacted Redacted 3 Redacted
2 John Smith 7 8
3 Redacted Redacted 9 Redacted
4 John Smith 12 20

Thank you.

CodePudding user response:

You may make a single assignment here:

cols <- c("FirstName", "LastName", "Age")
df[df$ID %in% IDList, cols] <- "Redacted"
df

  ID FirstName LastName Orders      Age
1  1  Redacted Redacted      3 Redacted
2  2      John    Smith      7        8
3  3  Redacted Redacted      9 Redacted
4  4      John    Smith     12       20

Data:

df <- data.frame(ID=c(1:4), FirstName=rep("John", 4),
                 LastName=rep("Smith", 4), Orders=c(3,7,9,12), 
                 Age=c(30,8,317,20), stringsAsFactors=FALSE)
IDList <- c(1,3,74,312)

CodePudding user response:

Here is a dplyr solution:

library(dplyr)
df %>% 
  mutate(across(c(FirstName, LastName, Age), ~ifelse(ID %in% IDList, "Redacted", .)))
  ID FirstName LastName Orders      Age
1  1  Redacted Redacted      3 Redacted
2  2      John    Smith      7        8
3  3  Redacted Redacted      9 Redacted
4  4      John    Smith     12       20

CodePudding user response:

Using simple base R

df$Age = ifelse(df$ID %in% IDList, 'Redacted', df$Age)

  ID FirstName LastName Orders      Age
1  1      John    Smith      3 Redacted
2  2      John    Smith      7        8
3  3      John    Smith      9 Redacted
4  4      John    Smith     12       20
  •  Tags:  
  • r
  • Related