Re: [R] Geting names of variables

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Sat, 22 Mar 2008 12:23:26 -0400 (EDT)


I don't quite understand what you doing but have you looked at ?names ?

 x1 <- 3
 x2 <- 5
 y <- -34.5
mylist <- list(x1,x2,y)
mynames <- c("First Trade Position", "Second Trade Position", "P&L")
names(mylist) <- mynames
mylist

> I wanted to create a list with the names of
> variables and their
> values. So I wrote the following function.
>
> add.to.list.names.vars.1 <- function(lnv, vnv) {
> i <- 1
> while (i < length(vnv)) {
> z <-
>
as.character(eval(substitute(quote(vnv[i+1])))[[2]][[i
> + 2]])
> lnv[[vnv[i]]] <- list(Name = z, Value =
> vnv[i+1])
> i <- i + 2
> }
>
> lnv
> }
>
> It works, but it is very cumbersome. I wonder if
> there is a smarter
> way to do it. Here is an example:
>
> > my.list <- list()
> > x1 <- 3
> > x2 <- 5
> > y <- -34.5
> > my.list <- add.to.list.names.vars.1(my.list,
> c("First Trade Position", x1, "Second Trade
> Position", x2, "P&L", y))
> > my.list
> $`First Trade Position`
> $`First Trade Position`$Name
> [1] "x1"
>
> $`First Trade Position`$Value
> [1] "3"
>
>
> $`Second Trade Position`
> $`Second Trade Position`$Name
> [1] "x2"
>
> $`Second Trade Position`$Value
> [1] "5"
>
>
> $`P&L`
> $`P&L`$Name
> [1] "y"
>
> $`P&L`$Value
> [1] "-34.5"
>
> ______________________________________________
> R-help_at_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_at_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. Received on Sat 22 Mar 2008 - 16:51:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 22 Mar 2008 - 17:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive