Dictionaries and pairs

前端 未结 5 1951
时光取名叫无心
时光取名叫无心 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:16

    Here's how you'd do this in a data.table, which will be fast and easily extendable:

    library(data.table)
    d = data.table(i = 1, j = 2:5, value = c(1,3,4,3), key = c("i", "j"))
    
    # access the i=1,j=3 value
    d[J(1, 3)][, value]
    
    # change that value
    d[J(1, 3), value := 12]
    
    # do some vector assignment (you should stop thinking loops, and start thinking vectors)
    d[, value := i * j]
    

    etc.

提交回复
热议问题