[R] Naming Lists

Charles C. Berry cberry at tajo.ucsd.edu
Tue Aug 7 18:17:57 CEST 2007


On Tue, 7 Aug 2007, Tom.O wrote:

>
> Hi
> Im pretty new to R and I have run in to a problem. How do I name all the
> levels in this list.

One way:

MyList <-
   mapply(function(x) mapply(
                             function(y) mapply( function(z)
 				paste(unlist(x),unlist(y),unlist(z)),
                                                Lev3, SIMPLIFY=FALSE  ),
                             Lev2, SIMPLIFY=FALSE   ),
          Lev1, SIMPLIFY=FALSE )


However, for many purposes you might better use an array:

MyArray.dimnames <- list(Lev1,Lev2,Lev3)
combos <-  rev( expand.grid (Lev3,Lev2,Lev1) )
elements <- do.call( paste, combos )
MyArray <- array( elements, dim=sapply(MyArray.dimnames,length), dimnames=MyArray.dimnames )
MyArray['A1','B1','C2']



>
> Lev1 <- c("A1","A2")
> Lev2 <- c("B1","B2")
> Lev3 <- c("C1","C2")
>
> MyList <- lapply(Lev1,function(x){
> lapply(Lev2,function(y){
> lapply(Lev3,function(z){
> paste(unlist(x),unlist(y),unlist(z))
> })})})
>
> I would like to name the different levels in the list with the differnt
> inputs.
> So that the first level in named by the inputs in Lev1 and the second Level
> by the inputs in Lev2, and so on
>
> I would then like to use this call
>
> MyList$A1$B1$C1
>> "A1 B1 C1"
>
> Regards Tom
> -- 
> View this message in context: http://www.nabble.com/Naming-Lists-tf4228968.html#a12030717
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list