[R] using assign with lists
jim holtman
jholtman at gmail.com
Tue Oct 7 17:55:35 CEST 2008
Here is something to try:
> result <- list()
> for (i in 1:2){
+ for (j in 1:2){
+ result[[paste(i, j, sep="|")]] <- rep(i,j)
+ }
+ }
>
> result
$`1|1`
[1] 1
$`1|2`
[1] 1 1
$`2|1`
[1] 2
$`2|2`
[1] 2 2
On Tue, Oct 7, 2008 at 11:01 AM, Mark Kimpel <mwkimpel at gmail.com> wrote:
> I am performing many permutations on a data-set with each permutation
> producing a variable number of results. I thought that the best way to keep
> track of all this in one object would be with a list ('res.lst'). To address
> these variable results for each permutation I attempted to construct this
> list using 'assign'. There is even more nesting than indicated below, but
> this is a simple example that, if addressed, will fit answer my question.
> The below code chunk clearly does not produce the desired results because,
> instead of assigning a new vector to the list, it creates a new variable
> 'res.list$contrast.i.j' . In the last two lines I show what I really want to
> happen. Can I use assign in this context by using it differently?
>
> Thanks, Mark
>
> res.lst <- list()
> for (i in 1:2){
> for (j in 1:2){
> assign(paste("res.lst$contrast", i, j, sep = "."), paste(i,j,sep="."))
> }
> }
> res.lst
>
> ls(pattern = "res.lst..?")
>
> res.lst$contrast.5.5 <- 5.5
> res.lst
> ------------------------------------------------------------
> Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry
> Indiana University School of Medicine
>
> 15032 Hunter Court, Westfield, IN 46074
>
> (317) 490-5129 Work, & Mobile & VoiceMail
> (317) 399-1219 Home
> Skype: mkimpel
>
> ******************************************************************
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list