R, get key from key value (hash)

20,905

The names of elements are stored in an attribute called "names", which can be accessed via the names function.

Try this:

mylist <- list(width=42, height=13)
names(mylist)    # "width"  "height"
names(mylist)[1] # "width"

mylist[["width"]] # 42

mylist[sort(names(mylist))] # sort mylist by the names...
Share:
20,905
mariodeng
Author by

mariodeng

I like optimisation.

Updated on June 12, 2020

Comments

  • mariodeng
    mariodeng about 4 years

    quick question. My List (in R) looks like:

    > mylist
    $width
    [1] 32
    

    With mylist[1] I get:

    $width
    [1] 32
    

    But how do I get:

    $width
    

    Thanks and cheers.