Working with dictionaries/lists in R

后端 未结 7 1625
说谎
说谎 2020-12-07 09:14

I have trivial question: I couldn\'t find a dictionary data structure in R, so I used list instead (like \"word\"->number) So, right now I have problem how to get the list o

7条回答
  •  青春惊慌失措
    2020-12-07 09:50

    You do not even need lists if your "number" values are all of the same mode. If I take Dirk Eddelbuettel's example:

    > foo <- c(12, 22, 33)
    > names(foo) <- c("tic", "tac", "toe")
    > foo
    tic tac toe
     12  22  33
    > names(foo)
    [1] "tic" "tac" "toe"
    

    Lists are only required if your values are either of mixed mode (for example characters and numbers) or vectors.

    For both lists and vectors, an individual element can be subsetted by name:

    > foo["tac"]
    tac 
     22 
    

    Or for a list:

    > foo[["tac"]]
    [1] 22
    

提交回复
热议问题