[R--gR] R function to create a list

Achim Zeileis zeileis at ci.tuwien.ac.at
Mon Oct 13 15:42:46 CEST 2003


On Monday 13 October 2003 15:32, Thomas, Andrew wrote:

> Hi gRs,
>
>  does anyone know how to write a R function which creates a list
> with elements name0, name1... nameN where the names are strings? I
> have found a way of creating R objects to represent name0, name1 etc
> in the global scope but am suffering a bit from name space problems.
> What I want is a way of setting up a corespondance between nodes in
> the graphical model and R objects.
> Things are sort of working for BRugs but I do not understand what is
> going on fully...

If I understand you correctly you want something like this:

create the list:
R> x <- list(1:10, rnorm(25), letters[1:3])

look at the list (no names yet):
R> x
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1]  0.93029227  0.75277557  0.78905723  0.92361961  0.72131147  
0.78590861
 [7]  1.55662879  1.69169126  0.52312714  0.38258802 -0.54113693 
-0.91252610
[13] -0.03990455 -0.17300226 -0.47568476 -0.04564297  0.68333195  
1.68230628
[19] -0.13403211 -0.89886517  0.27059495 -0.08171203  0.27128162 
-0.94402618
[25]  1.04557120

[[3]]
[1] "a" "b" "c"

assign names to the slots of the list
R> names(x) <- c("numbers", "normalsample", "someletters")

look at the list again (now with names)
R> x
$numbers
 [1]  1  2  3  4  5  6  7  8  9 10

$normalsample
 [1]  0.93029227  0.75277557  0.78905723  0.92361961  0.72131147  
0.78590861
 [7]  1.55662879  1.69169126  0.52312714  0.38258802 -0.54113693 
-0.91252610
[13] -0.03990455 -0.17300226 -0.47568476 -0.04564297  0.68333195  
1.68230628
[19] -0.13403211 -0.89886517  0.27059495 -0.08171203  0.27128162 
-0.94402618
[25]  1.04557120

$someletters
[1] "a" "b" "c"

and then you can also access some slot of x by its name
R> x$numbers
 [1]  1  2  3  4  5  6  7  8  9 10

hope that helps,
Z

> All the best
>
> 	Andrew
>
> _______________________________________________
> R-sig-gR mailing list
> R-sig-gR at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/r-sig-gr




More information about the R-sig-gR mailing list