Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find percentage in non zero columns

Tags:

r

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?

like image 663
Neil Avatar asked Mar 09 '23 05:03

Neil


1 Answers

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%)"          
like image 96
akrun Avatar answered Mar 25 '23 06:03

akrun