[R] Extending each element in a list, or rbind()-ing arrays of different length without recycling

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 13 19:27:32 CET 2009


That is very nice.  Maybe just one slight improvement so
to express it in a non-destructive form:

    replace(mat, t(apply(mat,1,duplicated)), NA)


On Fri, Feb 13, 2009 at 12:58 PM, Stavros Macrakis
<macrakis at alum.mit.edu> wrote:
> (typos corrected)
>
> Combining the various approaches on the list, here's a simple
> one-liner that puts the NAs at the end:
>
>    t(apply(mat,1,function(r) { dr<-duplicated(r); c( r[!dr],
> rep(NA,sum(dr)) ) }))
>
> If you don't care where the NAs are, the following is a tad shorter
> and perhaps clearer:
>
>    mat[ t(apply(mat,1,duplicated)) ] <- NA                  # modifies mat
>
>        -s
>
> On Fri, Feb 13, 2009 at 12:49 PM, Stavros Macrakis
> <macrakis at alum.mit.edu> wrote:
>> Combining the various approaches on the list, here's a simple
>> one-liner that puts the NAs at the end:
>>
>>     t(apply(mat,1,function(r) { dr<-duplicated(r); c( r[!dr],
>> rep(NA,sum(dr)) ) ))
>>
>> If you don't care where the NAs are, the following is a tad shorter
>> and perhaps clearer:
>>
>>     mat[ t(apply(mat,1,duplicated)) ] < -NA                  # modifies mat
>>
>>         -s
>>
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list