Re: [R] reexpand a matrix after subsetting

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 30 Aug 2005 - 02:18:45 EST

On 8/29/2005 12:11 PM, Ido M. Tamir wrote:
> Hi,
>
> suppose I have a matrix (or dataframe)
> as a result from subsetting.
>
> mat <- matrix(1:20,ncol=2)
> mat[c(3,6,9),] <- NA
> cc <- complete.cases(mat)
> sub <- mat[cc,,drop=FALSE]
> sub <- sub * 2
> #some caluculations with sub.
>
> now I would like to expand sub somehow
> so row 3,6, and 9 would be filled with
> NAs but the rest should be in place again.
> Is there a simple function for this?
>
> merge is not an option.
>
> Thank you very much for your help.

You just need to calculate the original row numbers. For example,

goodrows <- (1:nrow(mat))[cc]
mat[goodrows,,drop=FALSE] <- sub

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Aug 30 02:29:13 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:02:14 EST