Help Percentages

Hello everyone! Sorry, R and RStudio are just brand new for me and this question sure enough it's straightforward. I have just started writing, so it is still confusing for me, but I need to calculate percentages:

letters: phenomenon encountered
numbers: number of events
a - 28
b - 8
c - 3
d - 4
e - 3
f - 6
g - 5
h - 3
i - 7

Considering the total, 67, what are the relative percentages? How do I retrieve them using RStudio?
Thanks :pray:

Make a vector with labels. Something like

myData <- c(a=28,b=8) # etc.

Then

percentages <- myData/sum(myData)
1 Like

Hi welcome aboard.

Here are a couple of ways, the first one uses base R and the second uses the tidyverse approach. There usually are more than one way to do things in R.

Base

tt <- data.frame(
               alf = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
              numb = c(28L, 8L, 3L, 4L, 3L, 6L, 5L, 3L, 7L) )

tt$perc <- tt$numb / sum(tt$numb)

tidyverse

library(tidyverse)
dat <- data.frame(
  alf = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
  numb = c(28L, 8L, 3L, 4L, 3L, 6L, 5L, 3L, 7L) )

dat$perc <-  dat |> mutate(numb / sum(numb))

To use the second method you may need to install tidyverse

install.packages("tidyverse")
1 Like

Thank you so much! They both work! :pray:

If i want to obtain the percentage with one decimal, e.g., a= 41.8%?

a b c d e f g h
0.41791045 0.11940299 0.04477612 0.05970149 0.04477612 0.08955224 0.07462687 0.04477612
i
0.10447761

I've had some value from using library(formattable) with my own work.

library(tidyverse)
dat <- data.frame(
  alf = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
  numb = c(28L, 8L, 3L, 4L, 3L, 6L, 5L, 3L, 7L) )

library(formattable)
(dat <-  dat |> mutate(perc = percent(numb / sum(numb),digits=1)))
  alf numb  perc
1   a   28 41.8%
2   b    8 11.9%
3   c    3  4.5%
4   d    4  6.0%
5   e    3  4.5%
6   f    6  9.0%
7   g    5  7.5%
8   h    3  4.5%
9   i    7 10.4%
1 Like

Thank u so much for your help! :relaxed:

On the whole, I think I prefer nirgrahamuk'
approach as it in cleaner but you also can do this:

tt <- data.frame(
               alf = c("a", "b", "c", "d", "e", "f", "g", "h", "i"),
              numb = c(28L, 8L, 3L, 4L, 3L, 6L, 5L, 3L, 7L) )
tt$perc <- round(tt$numb / sum(tt$numb), 1)
1 Like

Thank u for your time, really!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.