I have following dataframe in R
ID IT FMCG CDGS
A 0 20 50
B 10 30 67
C 23 0 40
I want percentage share of non zero columns and print in new column. e.g FMCG (20/(20+50)) 28%
Desired R dataframe
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%)
and so on, I am using following code to print the non zero column names
simplyfy2array(apply(df[2:4],1,function(x)paste(names(df[2:4])[x!=0],collapse="")))
How to add percentage in above code?
We can use apply
with MARGIN = 1
to loop over the rows, subset the elements that are not 0, divide the by sum
of elements to create the percentage and paste
with the names
df1$perc <- apply(df1[-1], 1, FUN = function(x) {
x1 <- x[x!=0]
if(length(x1)==0) {
""} else {
x2 <- round(100*x1/sum(x1))
paste0(paste(names(x2), paste0(x2, "%"), sep="(", collapse="), "), ")")}})
df1$perc
#[1] "FMCG(29%), CDGS(71%)" "IT(9%), FMCG(28%), CDGS(63%)" "IT(37%), CDGS(63%)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With