On 12/03/2010 7:20 AM, Søren Højsgaard wrote:
Rune,
I doubt that this is possible because R-objects will typically not know "their own 
name".

Objects don't know their own name, but functions can find out everything about how they were called. So something like this would come close:

mylist <- function(..., use.var.names=FALSE) {
 vals <- list(...)
 if (use.var.names) {
   names <- names(vals)
   call <- sys.call()
   skip <- 1
   for (i in 2:length(call)) {
     if (names(call)[i] == "use.var.names") skip <- 2
     else if (names(call)[i] == "")
       names[i-skip] <- deparse(call[[i]])
   }
   names(vals) <- names
 }
 vals
}
I haven't tested it much, so it probably has bugs.

Duncan Murdoch

Med venlig hilsen / Regards Søren Højsgaard


-----Oprindelig meddelelse-----
Fra: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] På 
vegne af Rune Schjellerup Philosof
Sendt: 12. marts 2010 10:46
Til: Linlin Yan
Cc: r-help@r-project.org
Emne: Re: [R] Creating named lists

No, I mean this:
a <- 1
b <- 2
list(a=a, b=b)

I just find it anoying, that I have to type the names of the variables twice.
I would like something like this instead:
list(a, b, use.var.names=TRUE)

--
Rune

Linlin Yan wrote:
> Did you mean this:
>
> >> n <- c('a', 'b')
>> structure(list(1, 2), names = n)
>> > $a
> [1] 1
>
> $b
> [1] 2
>
>
> On Fri, Mar 12, 2010 at 5:28 PM, Rune Schjellerup Philosof > <rphilo...@health.sdu.dk> wrote: > >> I often find myself making lists similar to this list(var1=var1, >> var2=var2)
>>
>> It doesn't seem list has an option, to make it use the name of the >> variable as name in the list.
>> Is there another function that does this?
>>
>> --
>> Med venlig hilsen
>>
>> Rune Schjellerup Philosof
>> Ph.d-stipendiat, Forskningsenheden for Biostatistik
>>
>> Telefon: 6550 3607
>> E-mail:  rphilo...@health.sdu.dk
>> Adresse: J.B. Winsløwsvej 9, 5000 Odense C
>>
>> SYDDANSK UNIVERSITET
>> _______________________________________________________________
>> * Campusvej 55 * 5230 * Odense M * 6550 1000 * www.sdu.dk
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> 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.
>>
>>
--
Med venlig hilsen

Rune Schjellerup Philosof
Ph.d-stipendiat, Forskningsenheden for Biostatistik

Telefon: 6550 3607
E-mail:  rphilo...@health.sdu.dk
Adresse: J.B. Winsløwsvej 9, 5000 Odense C

SYDDANSK UNIVERSITET
_______________________________________________________________
* Campusvej 55 * 5230 * Odense M * 6550 1000 * www.sdu.dk


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.


______________________________________________
R-help@r-project.org mailing list
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 via email to