I have the following data frame in R
ID IT FMCG CDGS
A 0 20 50
B 10 30 67
C 23 0 40
I want a percentage of non-zero columns and print in a new column. e.g. FMCG (20 / (20 + 50)) 28%
Required data frame R
ID IT FMCG CDGS Perc_Portfolio
A 0 20 50 FMCG(28%),CDGS(72%)
B 10 30 67 IT(10%),FMCG(28%),CDGS(62%)
C 23 0 40 IT(36%),CDGS(64%)
etc., I use the following code to print non-zero column names
simplyfy2array(apply(df[2:4],1,function(x)paste(names(df[2:4])[x!=0],collapse="")))
How to add a percentage to the code above?
source
share