What is the difference between a list and a pairlist in R?

后端 未结 2 971
死守一世寂寞
死守一世寂寞 2020-12-13 23:49

In reading the documentation for lists, I found references to pairlists, but it wasn\'t clear to me how they were different from lists.

2条回答
  •  难免孤独
    2020-12-14 00:36

    First of all, pairlists are deprecated

    pairlists are deprecated for normal use because "generic vectors" are typically more efficient. You won't ever need to worry about them unless you are working on R internals.


    lists can contain named elements

    Each element in a list in R can have a name. You can access each element in a list either by name or by its numerical index.

    Here is an example of a list in which the second element is named 'second':

    > my.list <- list('A',second='B','C')
    > my.list
    [[1]]
    [1] "A"
    
    $second
    [1] "B"
    
    [[3]]
    [1] "C"
    

    All elements can be indexed by its position in the list. Named elements can additionally be accessed by name:

    > my.list[[2]]
    [1] "B"
    > my.list$second
    [1] "B"
    

    Also, each element in a list is a vector, even if it is only a vector containing a single element. For more about lists, see How to Correctly Use Lists in R?.


    pairlists can contain empty named elements

    A pairlist is basically the same as a list, except that a pairlist can contain an empty named element, but a list cannot. Also, a pairlist is constructed using the alist function.

    > list('A',second=,'C')
    Error in as.pairlist(list(...)) : argument is missing, with no default
    > alist('A',second=,'C')
    [[1]]
    [1] "A"
    
    $second
    
    
    [[3]]
    [1] "C"
    

    But, as mentioned earlier, they are deprecated. They do not have any benefit or advantage over lists that I know of.

提交回复
热议问题