How to create a dictionary / hash table by iterating through a column?

前端 未结 3 507
广开言路
广开言路 2020-12-29 10:35

I have a data frame of two columns: key and value and I would like to create a dictionary using the respective row of each column for each element of the dictionary / hash t

3条回答
  •  粉色の甜心
    2020-12-29 11:09

    It appears to me you've gotten some misinformation. I'm not even certain where you get the idea of that syntax for creating a hashtable.

    In any case: for hashtable-like functionality, you may want to consider using an environment: these work internally with a hashtable (if I remember correctly), so do quite what you want to.

    You would use this something like:

    someenv<-new.env()
    someenv[["key"]]<-value
    

    Given your data.frame, something like this would fill it up:

    for(i in seq(nrow(lbls)))
    {
      someenv[[ lbls[i,1] ]]<- lbls[i,2]
    }
    

    (note: this requires that the first column is an actual character column, not a factor!!)

    You can then easily get to a named value by using someenv[["nameofinterest"]].

提交回复
热议问题