[R] loop

Luis Silva lm.silva at sapo.pt
Mon Apr 21 14:13:40 CEST 2003


Olá (em português finalmente)

Já estive a experimentar com um pequeno exemplo e parece que 
funciona. Vou agora aplicar ao que estou a fazer e depois dou 
notícias. O uso de sapply e variantes era para evitar o loop, 
mas quando quero usar os resultados é que surgem os problemas.

obrigado e boa pascoa
luis

} Olá
} 
} Podes criar a lista antes do loop (aliás é
} aconselhável que o faças para
} poupar tempo de computação) e "apontas" os resultados
} do loop para os
} elementos da lista.
} 
} lst <- list()
} vec <- seq(0,1,0.1)
} length(lst)<-length(vec)
} for(i in 1:length(vec)){
} 
} ...
} 
} lst[[i]] <- ...
} 
} }
} 
} Espero que ajude
} 
} EJ
} 
} 
} PS: The answer is in portuguese.
} 
} On Mon, 2003-04-21 at 11:03, Luis Silva wrote:
} > I tried the tips from Sundar but I  still have
} problems. I want 
} > both values and vectors, and the latter will be
} used to project 
} > my original data. The problem is that with those
} command lines 
} > I can't get them as a matrix and when I try
} as.matrix it 
} > doesn't work. With only vectors
} > 
} > > b<-sapply(my.list, function(x) eigen(x)$vectors)
} > > attributes(b)
} > $dim
} > [1] 65536    11
} > 
} > $dimnames
} > $dimnames[[1]]
} > NULL
} > 
} > $dimnames[[2]]
} >  
} > [1] "0"   "0.1" "0.2" "0.3" "0.4" "0.5" "0.6" "0.7"
} "0.8" "0.9" 
} > "1"  
} > 
} > 
} > > b[1]
} > [1] 0.02533057+0i
} > 
} > thanks
} > luis
} > 
} > } 
} > } 
} > } Luis Silva wrote:
} > } > Dear helpers
} > } > 
} > } > I have this problem. I want to make a linear
} > } combination a*A+(1-
} > } > a)*B where A and B are matrices. I want that a
} be
} > } incremented 
} > } > from 0 to 1 by 0.1 so I made a loop with for.
} The
} > } problem is 
} > } > that I want to keep the result in an object or
} list
} > } or 
} > } > something like that and then apply eigen
} > } decomposition to all 
} > } > of the resulting matrices
} > } > 
} > } > sapply(my.list,eigen)
} > } > 
} > } > the problem is that I don't know how to build
} that
} > } list in the 
} > } > loop. I tried several things but it doesn't
} work
} > } (in Matlab i 
} > } > can do it)
} > } > 
} > } 
} > } a <- seq(0, 1, 0.1)
} > } my.list <- lapply(a, function(a, A, B) a*A +
} (a-1)*B,
} > } A=A, B=B)
} > } names(my.list) <- as.character(a)
} > } sapply(my.list, function(x) eigen(x)$value)
} > } 
} > } Regards,
} > } Sundar
} > } 
} > } 
} > 
} > --
} > 
} > 
} > http://adsl.sapo.pt
} > 
} > ______________________________________________
} > R-help at stat.math.ethz.ch mailing list
} >
} https://www.stat.math.ethz.ch/mailman/listinfo/r-help
} 
} 

--


http://adsl.sapo.pt



More information about the R-help mailing list