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

前端 未结 3 506
广开言路
广开言路 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:10

    The easiest way is to change names after creating variables. So you can define a function like this:

    cc <- function(name, value) {
        ret <- c(value)
        names(ret) <- name
        ret
    }
    
    cc(c(letters[1:2], "a name"), c(LETTERS[1:2], "a value"))
    
    # output like this
    #    a         b    a name 
    #   "A"       "B" "a value" 
    

提交回复
热议问题