I have two datasets, one where the key variable is unique on each row and another where the key variable repeats on a number of rows; like in this example data:
set.seed(300)
data1 <- data.frame(key = LETTERS[seq( from = 1, to = 5 )], value = rnorm(5,1,1))
data2 <- data.frame(key = rep(LETTERS[seq( from = 1, to = 5 )], 2), value = rnorm(10,1,1))
I want a function to recode the value
variable on data1
according to the minimum value of the value
variable on data2
.
To do so, I've been trying to repeat it on each key
value, like this:
data1$value[data1$key == "A"] <- min(range(data2$value[data2$key == "A"]))
data1$value[data1$key == "B"] <- min(range(data2$value[data2$key == "B"]))
data1$value[data1$key == "C"] <- min(range(data2$value[data2$key == "C"]))
data1$value[data1$key == "D"] <- min(range(data2$value[data2$key == "D"]))
data1$value[data1$key == "E"] <- min(range(data2$value[data2$key == "E"]))
I know there must be a bunch of different ways to accomplish this, but I haven't found the way (maybe using dplyr?) and I would like to avoid a for loop.
Thanks in advance!
EDIT:
I don't want to dplyr::summarise
a new dataframe; I need to keep the other variables present in data1
(yes, originally there are more variables than I put on here).