Search code examples
rmatrixdata-management

Resorting columns to initial ordering after deletion of columns


Clumsy title, but let me explain what's in it:

My initial matrix looks like that:

kitty <- matrix(  
  c(1, 2, 4, 0, 0, 0, 0, 0, 0, 3, 1, 2), 
  nrow=3, 
  ncol=4)

returning:

X1 X2 X3 X4
1  0  0  3
2  0  0  1
4  0  0  2

I delete all columns of zeros

kitty<- kitty[, colSums(kitty != 0) > 0]

as I have to run a particular set of econometrics over the matrix which yields (complete random set of numbers here, important is that columns 2 and 3 are no longer in it and that my methodology does not allow me to name columns either):

kitty2 <- matrix(  
  c(2, 3, 4, 1, 3, 8), 
  nrow=3, 
  ncol=2)

X1 X4
2  1
3  3
4  8

What is an efficient way (I have hundreds of those matrices) to reset columns back to their initial position, filling the missing columns with NAs or 0s?

X1 X2 X3 X4
2  NA NA  1
3  NA NA  3
4  NA NA  8

Solution

  • So frankly I am not so sure that this is particularly efficient, but it is the best way I can think of to do it:

    Grab the nonzero indices of kitty:

    indices <- which(colSums(kitty != 0) > 0)
    

    And then once you have kitty2, refill kitty with the values of the columns you changed.

    kitty[,indices] <- kitty2
    kitty
             [,1] [,2] [,3] [,4]
       [1,]    2    0    0    1
       [2,]    3    0    0    3
       [3,]    4    0    0    8
    

    And then you could leave the columns as zeroes or change them to NA.