I'm sure there must be an easy answer to that, but I'm struggling nonetheless and some research did not help me finding what I want. Let's say I have some named vector with "keys" (technologies) and "values" (values) (maybe this is not a good-enough structure and I should try to find how to create a dictionnary with some other package ?)
technology <- c("old", "medium", "modern")
yields <- c(0.7, 0.8, 0.9)
names(yields) <- technology
print(yields)
old medium modern
0.7 0.8 0.9
Now I have a dataframe as follows
tech <- c("old", "modern", "modern", "medium", "old")
consumption <-c(100, 230, 120, 80, 130)
df <- data.frame(tech, consumption)
print(df)
tech consumption
1 old 100
2 modern 230
3 modern 120
4 medium 80
5 old 130
I would then like to create a new column which retrieved the corresponding yield value to the dataframe. Something similar to this:
tech consumption yield
1 old 100 0.7
2 modern 230 0.9
3 modern 120 0.9
4 medium 80 0.8
5 old 130 0.7
I know I could do that using some joints, but I'm not sure this is an effective way of doing that if I have thousands of rows. I would really appreciate some help about the simplest/most effective way of doing this kind of mapping. I lack the experience with R syntax to simply go along and try randomly creating some dictionnaries (with the dict package for instance) and applying it to the dataframe...
Thanks a lot in advance, I hope the example was clear enough.