Home > Software design >  calculate percentage in a data frame R
calculate percentage in a data frame R

Time:06-17

I have a data frame of multiple columns. I want to create a new colomns with the percentage. I don't have the correct output, any suggestions?

il.count.descrition elle.count.description un.count.description   Words.count
     5                      1                      5                    563
     9                      2                      2                    65
     1                      1                      4                    100
     10                     9                      0                    89
data %>% mutate(il.count.ratio = il.count.description / Words.count * 100,
                elle.count.ratio = elle.count.description / Words.count * 100,
                un.count.ratio = un.count.description / Words.count * 100)

CodePudding user response:

You can try this, it works for me. Maybe the error is because you have a typo in the first variable name.

data <- data %>% mutate(il.count.ratio = round(il.count.descrition / Words.count * 100,1),
                elle.count.ratio = round(elle.count.description / Words.count * 100,1),
                un.count.ratio = round(un.count.description / Words.count * 100,1))
  • Related