I'd like to be able to replace a subset of element values in a vector within my data.frame object in R. Toy examples I've found thus far are simple (and small) enough to manually type and replace the few elements you want to target with those you want to replace. While this toy example will again be small enough to manually type the target and replacement elements, I'm hoping it serves as an easy representation in which there are many more unique names, yet the problem remains the same:
SampleID <- rep(c("Keith", "Mick", "Brian", "Ronnie"), times = 3)
Trial <- sort(rep(c(1,2,3), times = 4))
set.seed(10)
Scores <- sample.int(100, 12)
df <- data.frame(SampleID, Trial, Scores)
Now take this example and extend it way out to include thousands of unique individual SampleID names; let's say this study actually has a list of like 5000 unique individuals, and your dataset needed to be recoded such that 100 individuals needed to be renamed.
Is there a way utilize two vectors that represent the lists of identified target
names you want to replace with the replacement
names you want to recode with, without having to type something like:
df$SampleID <- recode(df$SampleID, "Mick" = "jagger", ... 99 other "target" = "replacement" values)
Perhaps the trick is to iterate with a for loop?
Many thanks.
I would recommend creating a reference data frame that contains target
and replacement
fields, like so:
new_df <- data.frame(target = 'Mick', replacement = 'Jagger')
Then you can merge this into your current df
:
df <- merge(df, new_df, by.x = 'SampleID', by.y = 'target', all.x = TRUE)
Then it's just a matter of using an ifelse()
statement to replace values in SampleID
with the values in replacement
where !is.na(replacement)
:
df$SampleID <- ifelse(!is.na(df$replacement), df$replacement, df$SampleID)