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