Home > Software engineering >  Take percentages conditional on a range
Take percentages conditional on a range

Time:03-24

I have a dataframe consisting of a single column as represented below. I would like to get the percentage of observations such that:

  • tenure is between 0 and 3
  • tenure is between 3 and 6
  • tenure is between 6 and 12
  • tenure is between 12 and 24
  • tenure is greater than 24

My hope is to get 5 numbers such that they add up to 100 (or 1).

tenure
2
3
1
4
0.5
3
7
8
9
3
1
2.5
6
4
4.5
5

CodePudding user response:

library(sur)
sur::percent.table(cut(df$tenure, breaks = c(0,3,6,12,24, Inf)))
(0,3]    (3,6]   (6,12]  (12,24] (24,Inf] 
50.00    31.25    18.75     0.00     0.00 
  • Related