define a list with names as variables

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

define a list with names as variables

Giovanni Gherdovich
Hello,

I'm having troubles defining a list where names are variables (of type
character). Like this, which gives "foo" instead of "world" (the way I
meant it is that "world" is the value of the variable foo). Any hint?

> f <- function(foo, bar) { list(foo = bar) }
> x <- f("hello", "world")
> names(x)
[1] "foo"


Thanks,
Giovanni

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Thomas Mailund-2
Do you mean like this?


> f <- function(foo, bar) {
+   result <- list(bar)
+   names(result) <- foo
+   result
+ }

> (x <- f("hello", "world"))
$hello
[1] "world"

> names(x)
[1] "hello"


--
Thomas Mailund

On 4 August 2017 at 12.08.28, Giovanni Gherdovich ([hidden email])
wrote:

Hello,

I'm having troubles defining a list where names are variables (of type
character). Like this, which gives "foo" instead of "world" (the way I
meant it is that "world" is the value of the variable foo). Any hint?

> f <- function(foo, bar) { list(foo = bar) }
> x <- f("hello", "world")
> names(x)
[1] "foo"


Thanks,
Giovanni

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Ulrik Stervbo-2
In reply to this post by Giovanni Gherdovich
Hi Giovani,

I would create an unnamed list and set the names after.

Best,
Ulrik

On Fri, 4 Aug 2017 at 12:08 Giovanni Gherdovich <[hidden email]>
wrote:

> Hello,
>
> I'm having troubles defining a list where names are variables (of type
> character). Like this, which gives "foo" instead of "world" (the way I
> meant it is that "world" is the value of the variable foo). Any hint?
>
> > f <- function(foo, bar) { list(foo = bar) }
> > x <- f("hello", "world")
> > names(x)
> [1] "foo"
>
>
> Thanks,
> Giovanni
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Giovanni Gherdovich
In reply to this post by Thomas Mailund-2
Hello Thomas, Ulrik,

thanks for your suggestions.


Giovanni

On Fri, Aug 4, 2017 at 12:13 PM, Thomas Mailund
<[hidden email]> wrote:

> Do you mean like this?
>
>
>> f <- function(foo, bar) {
> +   result <- list(bar)
> +   names(result) <- foo
> +   result
> + }
>
>> (x <- f("hello", "world"))
> $hello
> [1] "world"
>
>> names(x)
> [1] "hello"

On Fri, Aug 4, 2017 at 12:14 PM, Ulrik Stervbo <[hidden email]> wrote:
> Hi Giovani,
>
> I would create an unnamed list and set the names after.
>
> Best,
> Ulrik

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Jeff Newmiller
In reply to this post by Ulrik Stervbo-2
You can wrap the list-creating function call (e.g. lapply) in a call to ?setNames, or you can use the ?map function from the purrr package.
--
Sent from my phone. Please excuse my brevity.

On August 4, 2017 3:14:44 AM PDT, Ulrik Stervbo <[hidden email]> wrote:

>Hi Giovani,
>
>I would create an unnamed list and set the names after.
>
>Best,
>Ulrik
>
>On Fri, 4 Aug 2017 at 12:08 Giovanni Gherdovich
><[hidden email]>
>wrote:
>
>> Hello,
>>
>> I'm having troubles defining a list where names are variables (of
>type
>> character). Like this, which gives "foo" instead of "world" (the way
>I
>> meant it is that "world" is the value of the variable foo). Any hint?
>>
>> > f <- function(foo, bar) { list(foo = bar) }
>> > x <- f("hello", "world")
>> > names(x)
>> [1] "foo"
>>
>>
>> Thanks,
>> Giovanni
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Bert Gunter-2
> f <- function(foo,bar) structure(list(bar),names =foo)

> f("hello","world")
$hello
[1] "world"

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Aug 4, 2017 at 6:12 AM, Jeff Newmiller <[hidden email]> wrote:

> You can wrap the list-creating function call (e.g. lapply) in a call to ?setNames, or you can use the ?map function from the purrr package.
> --
> Sent from my phone. Please excuse my brevity.
>
> On August 4, 2017 3:14:44 AM PDT, Ulrik Stervbo <[hidden email]> wrote:
>>Hi Giovani,
>>
>>I would create an unnamed list and set the names after.
>>
>>Best,
>>Ulrik
>>
>>On Fri, 4 Aug 2017 at 12:08 Giovanni Gherdovich
>><[hidden email]>
>>wrote:
>>
>>> Hello,
>>>
>>> I'm having troubles defining a list where names are variables (of
>>type
>>> character). Like this, which gives "foo" instead of "world" (the way
>>I
>>> meant it is that "world" is the value of the variable foo). Any hint?
>>>
>>> > f <- function(foo, bar) { list(foo = bar) }
>>> > x <- f("hello", "world")
>>> > names(x)
>>> [1] "foo"
>>>
>>>
>>> Thanks,
>>> Giovanni
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide
>>http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Giovanni Gherdovich
On Fri, Aug 4, 2017 at 4:25 PM, Bert Gunter <[hidden email]> wrote:
>> f <- function(foo,bar) structure(list(bar),names =foo)
>
>> f("hello","world")
> $hello
> [1] "world"
>
> Cheers,
> Bert

Thanks Bert, I didn't know about "structure()".

Giovanni

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: define a list with names as variables

Martin Maechler
In reply to this post by Giovanni Gherdovich
>>>>> Giovanni Gherdovich <[hidden email]>
>>>>>     on Fri, 4 Aug 2017 12:36:49 +0200 writes:

    > Hello Thomas, Ulrik,
    > thanks for your suggestions.

There's also the
        setNames(<obj>, names)
which is a trivial wrapper to achieve this.
I had provided it for R (in package 'stats') just to help people
to write more nicely readable code:

Your function f()  below then becomes a simple one-liner:

   f <- function(foo, bar)  setNames(list(bar), foo)

Martin Maechler
ETH Zurich and R Core


    > Giovanni

    > On Fri, Aug 4, 2017 at 12:13 PM, Thomas Mailund
    > <[hidden email]> wrote:
    >> Do you mean like this?
    >>
    >>
    >>> f <- function(foo, bar) {
    >> +   result <- list(bar)
    >> +   names(result) <- foo
    >> +   result
    >> + }
    >>
    >>> (x <- f("hello", "world"))
    >> $hello
    >> [1] "world"
    >>
    >>> names(x)
    >> [1] "hello"

    > On Fri, Aug 4, 2017 at 12:14 PM, Ulrik Stervbo <[hidden email]> wrote:
    >> Hi Giovani,
    >>
    >> I would create an unnamed list and set the names after.
    >>
    >> Best,
    >> Ulrik

    > ______________________________________________
    > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    > https://stat.ethz.ch/mailman/listinfo/r-help
    > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    > and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.