Dictionaries and pairs

前端 未结 5 2027
时光取名叫无心
时光取名叫无心 2021-01-17 06:53

In R I was wondering if I could have a dictionary (in a sense like python) where I have a pair (i, j) as the key with a corresponding integer value. I have not

5条回答
  •  误落风尘
    2021-01-17 07:19

    You can just use a data.frame:

    a = data.frame(spam = c("alpha", "gamma", "beta"), 
                   shrub = c("primus","inter","parus"),
                   stringsAsFactors = FALSE)
    rownames(a) = c("John", "Eli","Seth")
    > a
          spam  shrub
    John alpha primus
    Eli  gamma  inter
    Seth  beta  parus
    
    >     a["John", "spam"]
    [1] "alpha"
    

    This handles the case with a 2d dictionary style object with named keys. The keys can also be integers, although they might have to be characters in stead of numeric's.

提交回复
热议问题