From olafm at statistik.tu-dortmund.de  Wed Oct 13 21:34:24 2010
From: olafm at statistik.tu-dortmund.de (Olaf Mersmann)
Date: Wed, 13 Oct 2010 21:34:24 +0200
Subject: [Rd] list comprehension to create an arbitrary-sized list with
	arbitrary names/values
In-Reply-To: <AANLkTik7xjr5TaH1AcFtg5cUJkTg2cfEPGy8i-RT69GY@mail.gmail.com>
References: <AANLkTik7xjr5TaH1AcFtg5cUJkTg2cfEPGy8i-RT69GY@mail.gmail.com>
Message-ID: <9F6ACDD6-9002-471B-AF6E-50131B6FE7C4@statistik.tu-dortmund.de>

Hi,

On 13.10.2010, at 21:26, Steve Kim wrote:
> mydict = dict([(keyfun(x), valfun(x)) for x in mylist])
> 
> to create a dictionary with whatever keys and values we want from an
> input list of arbitrary size. In R, I want to similarly create a list
> with names/values that are generated by some keyfun and valfun
> (assuming that keyfun is guaranteed to return something suitable as a
> name). How can I do this?

Try something like this:

  mydict <- lapply(mylist, valfun)
  names(mydict) <- sapply(mylist, keyfun)

or

  mydict <- structure(lapply(mylist, valfun), names=sapply(mylist, keyfun))

Cheers
Olaf



[Rd] list comprehension to create an arbitrary-sized list with arbitrary names/values
Olaf Mersmann