当一列不是数字时计算行的百分比权重

Compute percentage weights on rows when one column is not numeric

我调用了这个数据:

 Dates      Consumer    Staples Energy Financials Health Care

1 99 年 12 月 31 日 0 0 0 0 0 2 03/31/00 0 0 0 0 0 3 06/30/00 0 0 0 0 0 4 09/30/00 0 0 0 0 0 5 12/31/00 0 0 0 0 0 6 03/31/01 1000 0 0 50 0 7 06/30/01 0 0 0 0 0

我想计算每行每个类别的权重 但需要避免对作为日期的第一列求和 权重 <- round(out[2:6]/rowSums(out[2:6])*100, 2)

1/ 有没有办法将日期保留在第一列中,并计算 同一数据集中接下来 5 列的权重

2/ 当一个日期只有0条数据时,如何避免NA?

谢谢你的帮助

outN <- out[,-1]
rownames(outN) <- out[,1]
Cap_Weights <- round(outN/rowSums(outN)*100, 2)

Cap_Weights[is.na(Cap_Weights)] <- 0