[R] Naming elements of a list

Bernardo Rangel Tura tura at centroin.com.br
Thu Nov 22 09:44:48 CET 2007


On Thu, 2007-11-22 at 03:16 -0500, Thomas L Jones, PhD wrote:
> I have a numeric vector of lenth 1. I am trying to use it inside
> a function just by giving its name, rather than specifying it as
> an argument to the function. I am aware that there is an attach
> function which you need to call. The attach function will accept a
> list. However, I don't seem to be able to create the list properly. (Or 
> should I use a frame instead?)
> 
> free_driver <- function (){
> i <- numeric (1)
> attach (as.list (i))
> i <- 25
> 
> free_test ()
> 
> }
> free_test <- function (){
> print ("i =")
> print (i)
> return ()
> 
> }
> 
> Anyway, here is the output, starting with the load operation:
> 
> ------------------------------------------------------------------
> 
> > free_driver <- function (){
> + i <- numeric (1)
> + attach (as.list (i))
> + i <- 25
> +
> + free_test ()
> +
> + }
> > free_test <- function (){
> + print ("i =")
> + print (i)
> + return ()
> +
> + }
> > free_driver ()
> Error in attach(as.list(i)) : all elements of a list must be named
> >
> --------------------------------------------------------------
> 
> Is there an easy way to name all elements of a list?
> 
> Your advice?

Hi Tom,

If I understand your question is possible solve yours problem with this
code

free_driver <- function (){
i<-as.numeric(25)
i <- list(i)
free_test (i)
}


free_test <- function (i){
cat ("i =",i[[1]],"\n")
}


free_driver()


-- 
Bernardo Rangel Tura, M.D,Ph.D
National Institute of Cardiology
Brazil



More information about the R-help mailing list