1

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.

mikacrem
  • 37
  • 6

1 Answers1

1

merge or joints is going to be most effective I think. You can convert named vector to dataframe and then merge :

merge(df, stack(yields), by.x = 'tech', by.y = 'ind')

Keeping them as vector you can add a new column to df by subsetting the vector by it's name.

df$yield <- yields[df$tech]
df
#    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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you very much. However, if there was to be a more efficient way of doing that using dictionnaries (or similar objects) I would love to read about it. – mikacrem Dec 08 '20 at 07:30
  • 1
    Dictionary is not a basic data type in R. Lists are similar to dictionary but in this case it would be same as named vector that you already have. – Ronak Shah Dec 08 '20 at 07:37