[R] Naming Lists
Tom.O
tom.olsson at dnbnor.com
Wed Aug 8 07:16:21 CEST 2007
Thanks,
I have considered using array but I dont belive its possible. For my
function isn't really paste(), its a custom function that takes the three
different parameters. The result however is a matrix where the size depends
on the inputs. I belive thats hard to fit into an array. But if you have any
suggestions how I could improve the code. Felle free to pass them over, I
would be glad for anything.
//Tom
Charles C. Berry wrote:
>
> 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
>
> ______________________________________________
> 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.
>
>
--
View this message in context: http://www.nabble.com/Naming-Lists-tf4228968.html#a12047294
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list