Skip to content
Back to formatted view

Raw Message

Message-ID: <4788A2B3-40C3-41DB-A7AC-2335788AF92E@auckland.ac.nz>
Date: 2009-02-12T20:59:40Z
From: Rolf Turner
Subject: Extending each element in a list, or rbind()-ing arrays of different  length without recycling
In-Reply-To: <1574452236.10732631234469181782.JavaMail.javamailuser@localhost>

On 13/02/2009, at 9:06 AM, markleeds at verizon.net wrote:

> Hi Jason: below seems to work. you have to take the transpose because
> the apply
> returns the rows transposed. i'm also not sure how to make the NAs be
> the last
> ones but maybe someone can show us how to do that.

Pretty easy:

na.at.end <- function(x){
i <- is.na(x)
c(x[!i],rep(NA,sum(i)))
}

>
> mat <- matrix(c(2,7,2,7,9,10,10,6,8,6,1,9,7,2,0),byrow=TRUE,nrow=3)
> print(mat)
>
> t(apply(mat,1, function(.row) {
>    .row[duplicated(.row)] <- NA
>    .row
> }))

Then just change to:

t(apply(mat,1, function(.row) {
    .row[duplicated(.row)] <- NA
    na.at.end(.row)
}))

	cheers,

		Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}