Replacing values in one dataframe that match another

Hi, first post here so apologies if I offend anyone by my ignorance.

I am struggling to subset the rows of my data frame based on values in another data frame.

I wish to subset my data frame (df1) using values from my other data frame (df2).

For the rows that match on drug name, I wish to change all values in column df1$strength to 'all'.

Here is a reprex

df1 <- data.frame(Drugs = c("Drug A","Drug A","Drug B"), strength = c(50,100,300))

df2 <- data.frame(Drugs = c("Drug A","Drug X","Drug Z")

I wish to overwrite the strength values in df1 based on the presence of the drugs in df2.

The output of df2 should look as follows:

df1 <- data.frame(Drugs = c("Drug A","Drug A","Drug B"), strength = c(all,all,300))

Hi @jgarrigan,

Here is one way to do this using some functions from the {dplyr} package. It is worth noting that you can't combine the string "all" with a numeric value like 300, since R data.frames constrain a column vector to be the same data type. So I changed the strength variable in df1 to be a character vector, instead.

library(dplyr)

df1 <- data.frame(Drugs = c("Drug A","Drug A","Drug B"), strength = c("50", "100", "300"))
df2 <- data.frame(Drugs = c("Drug A","Drug X","Drug Z"))

df1 %>% 
  mutate(strength = if_else(Drugs %in% df2$Drugs, "all", strength))
#>    Drugs strength
#> 1 Drug A      all
#> 2 Drug A      all
#> 3 Drug B      300

Created on 2020-07-15 by the reprex package (v0.3.0)

Thanks Matt, I should have known this! Thanks again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.