Skip to content
Prev 258117 / 398502 Next

Assignments inside lapply

Here is a solution with lapply

PowerMatrix <- matrix(unlist(lapply(seq_len(dimx*dimy), function(x){
	i <- 1 + (x - 1) %% dimx
	j <- 1 + (x - 1) %/% dimy
	Pr(c(i,j),c(PRX,PRY),f)
})), nrow = dimx)

A reproducible example

dimx <- 5
dimy <- 6
PowerMatrix <- matrix(unlist(lapply(seq_len(dimx*dimy), function(x){
	i <- 1 + (x - 1) %% dimx
	j <- 1 + (x - 1) %/% dimy
	i * 10 + j
})), nrow = dimx)

Best regards,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey