On 26-03-2012, at 00:17, MBoersma wrote:

> Hi guys,
> 
> I'm quite new to R but quite enthousiastic. I'm trying to rewrite a bit of
> code in order to make it faster, so instead of nesting for loops I'm trying
> some apply functions. Since it's a combination of lists of matrices and
> other matrices, I would like to use lapply() on the list and still be able
> to use the list index to index in the matrices.
> 
> So, in "code format"-
> x <- list()
> for (i in 1:10){
>  x[[i]] <- c(1:i)
> }
> 

c(1:i) not necessary. Just 1:i will do.

> lapply(x,length)
> 
> is there a possible way to say "lapply(x, function(x) length(x)/index(x) )"
> and return 1's. I would be looking for the index. I know that row() and
> col() work in the matrix case. I would find it interesting to use it with
> arrays as well.

This may do what you want

zapply <- function(x) lapply(seq_len(length(x)), function(k) length(x[[k]])/k)

Berend

______________________________________________
R-help@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.

Reply via email to