[R] Geting names of variables

From: Fernando Saldanha <fsaldan1_at_gmail.com>
Date: Sat, 22 Mar 2008 12:12:29 -0400


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. Received on Sat 22 Mar 2008 - 16:25:56 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 - 19:31:18 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