Find percent in non-zero columns

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?

+4
source share
2 answers

We can use applyc MARGIN = 1to cycle through rows, a subset of elements that are not 0, divided by sumelements to create a percentage and pastewithnames

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%)"          
+4
source

Another idea is using dplyr,

library(dplyr)

df %>% 
 gather(val, var, -ID) %>% 
 filter(var != 0) %>% 
 group_by(ID) %>% 
 summarise(new = toString(paste0(val, '(', round(100*var/sum(var)), "%", ')')))

# A tibble: 3 × 2
#     ID                          new
#  <chr>                        <chr>
#1     A         FMCG(29%), CDGS(71%)
#2     B IT(9%), FMCG(28%), CDGS(63%)
#3     C           IT(37%), CDGS(63%)
+3
source

Source: https://habr.com/ru/post/1675891/


All Articles