Skip to content

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

5 messages · Jason Shaw, Jorge Ivan Velez, Gabor Grothendieck +1 more

#
Hi,

I'm trying to take a matrix such as

     [,1] [,2] [,3] [,4] [,5]
[1,]    2    7    2    7    9
[2,]   10   10    6    8    6
[3,]    1    9    7    2    0

and generate a new matrix which contains only the unique values in each row:

     [,1] [,2] [,3] [,4] [,5]
[1,]    2    7    9   NA   NA
[2,]   10    6    8   NA   NA
[3,]    1    9    7    2    0

My problem is that I can use apply(matrix,MARGIN=1,FUN=unique) to find
the unique values, but this leaves me with a list with arrays of
different length:
[[1]]
[1] 2 7 9

[[2]]
[1] 10  6  8

[[3]]
[1] 1 9 7 2 0

and using do.call("rbind",x) recycles the values of the shorter
vectors instead of filling them with NA:
[,1] [,2] [,3] [,4] [,5]
[1,]    2    7    9    2    7
[2,]   10    6    8   10    6
[3,]    1    9    7    2    0

So, I'd like to either take every element of the list and extend it
with NAs to the length of the longest element, or rbind every element
where missing places are filled with NAs instead of recycled values.
Is this possible?  Of course, the solution is trivial using a loop,
but I'm trying to avoid this.

Thanks for any suggestions.
#
Try this.  After the apply from your post we use lapply
to make each series into a zoo series so that we can later
use zoo's multiway merge.  Finally we actually merge them
and in the next statement just makes nice column names:
X1 X2 X3
1  2 10  1
2  7  6  9
3  9  8  7
4 NA NA  2
5 NA NA  0
On Thu, Feb 12, 2009 at 2:31 PM, Jason Shaw <jason.shaw.23 at gmail.com> wrote:
#
Here is one other solution.  Its nearly the same as the last but
uses ts instead of zoo:
Time Series:
Start = 1
End = 5
Frequency = 1
  X1 X2 X3
1  2 10  1
2  7  6  9
3  9  8  7
4 NA NA  2
5 NA NA  0

or unclass(all3) if  you don't want it as a ts object.  (Similar comment
for the zoo solution.)


On Thu, Feb 12, 2009 at 3:16 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
#
My question is: Why would you want a data
structure that is clearly not expressive of the
data involved?

Let me guess.  You are coming from statistical
software where data are always rectangular.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")
Jason Shaw wrote: