I have a table of dummy variables where the value is either 1 or NA. I know want to create a uniform weight for these dummies across rows. This is my beginning dataset, it is in xts format:
NESN ROG NOVN ZURN ABBN UBSG LONN
1989-12-01 1 NA 1 1 NA 1 NA
1990-01-01 1 NA 1 1 1 1 NA
I would then like to split the 1's so that the sum of the row is equal to 1. The ending dataset would then look like this:
NESN ROG NOVN ZURN ABBN UBSG LONN
1989-12-01 0.25 NA 0.25 0.25 NA 0.25 NA
1990-01-01 0.2 NA 0.2 0.2 0.2 0.2 NA
So if there are five stocks with a dummy equal to 1 in the whole row the 1 will be changed to 0.2. If there are 4 stocks it will be changed to 0.25 and so on. So far I was thinking of using replace to replace the 1's with their respective weight. However, I do not know how to do this over multiple rows with different weights. I am doing this so that I can ultimately calculate portfolio turnover.