rstatisticspercentage

Calculate percentage in a data frame R


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

il.count.description 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)

Solution

  • 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))