[R] row, col function but for a list (probably very easy question, cannot seem to find it though)
Berend Hasselman
bhh at xs4all.nl
Mon Mar 26 11:05:53 CEST 2012
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
More information about the R-help
mailing list