Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, get key from key value (hash)

Tags:

r

hash

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.

like image 339
mariodeng Avatar asked Apr 17 '12 20:04

mariodeng


People also ask

What is a hash value in R?

HASH VALUES can be any R value, vector or object. code {hash} returns a hash object. Key-value pairs may be specified via the ... argument as explicity arguments keys and values, as named key-value pairs, as a named vector or as implicit key, value vectors.

How are key-value pairs specified in Python?

Key-value pairs may be specified via the ... argument as explicity arguments keys and values, as named key-value pairs, as a named vector or as implicit key, value vectors. See examples below for each type. See .set for further details and how key-value vectors of unequal length are interpretted.

What is meant by pass by reference in R?

PASS-BY REFERENCE. Environments and hashes are special objects in R because only one copy exists globally. When provided as an argument to a function, no local copy is made. When passes to functions, those functions can change the value of the hash. This is not typical of R.


1 Answers

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...
like image 105
Tommy Avatar answered Sep 30 '22 17:09

Tommy