I have a dataset that has 6 rows and one column. The column contains the number 2 or 3. What i would like to do is count the number of times the 2 occurs and the 3 occurs then divide by the number of rows.
For example there are two 2s in the column so 2/6 = 33%.
I have used table(df['over1.5'])
to count the number of occurrences but not sure how to do divide the counts by the total number of rows.
CodePudding user response:
We may use proportions
on the table
output
round(100 *proportions(table(df$over1.5)))
2 3
33 67
Or with count
and divide by the sum
of frequencies
library(dplyr)
df %>%
count(over1.5) %>%
mutate(percentile = round(100*n/sum(n)))
over1.5 n percentile
1 2 2 33
2 3 4 67
data
df <- data.frame(over1.5 = c(2, 2, 3, 3, 3, 3))