-1

I have a dataset of Ages for the customer and I wanted to make a frequency distribution by 9 years of a gap of age.

Ages=c(83,51,66,61,82,65,54,56,92,60,65,87,68,64,51,
       70,75,66,74,68,44,55,78,69,98,67,82,77,79,62,38,88,76,99,
       84,47,60,42,66,74,91,71,83,80,68,65,51,56,73,55)

My desired outcome would be similar to below-shared table, variable names can be differed(as you wish)

Could I use binCounts code into it ? if yes could you help me out using the code as not sure of bx and idxs in this code?

binCounts(x, idxs = NULL, bx, right = FALSE)  ??


Age Count
38-46   3
47-55   7
56-64   7
65-73   14
74-82   10
83-91   6
92-100  3

Much Appreciated!

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user817995
  • 17
  • 1
  • 4

1 Answers1

1

I don't know about the binCounts or even the package it is in but i have a bare r function:

 data.frame(table(cut(Ages,0:7*9+37)))
      Var1 Freq
1  (37,46]    3
2  (46,55]    7
3  (55,64]    7
4  (64,73]   14
5  (73,82]   10
6  (82,91]    6
7 (91,100]    3

To exactly duplicate your results:

lowerlimit=c(37,46,55,64,73,82,91,101)
Labels=paste(head(lowerlimit,-1)+1,lowerlimit[-1],sep="-")#I add one to have 38 47 etc
group=cut(Ages,lowerlimit,Labels)#Determine which group the ages belong to
tab=table(group)#Form a frequency table
as.data.frame(tab)# transform the table into a dataframe
   group Freq
1  38-46    3
2  47-55    7
3  56-64    7
4  65-73   14
5  74-82   10
6  83-91    6
7 92-100    3

All this can be combined as:

data.frame(table(cut(Ages,s<-0:7*9+37,paste(head(s+1,-1),s[-1],sep="-"))))
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • May i know how to decide this `0:7*9+37` value – dondapati Feb 02 '18 at 07:16
  • 1
    oh, I just coded `data.frame(table(cut(Ages,c(37,46,55,64,73,82,91,101)))` which are the lower bounds for your table. But they have spaces of 9 apart. That's y i used a math formula. sorry for that – Onyambu Feb 02 '18 at 07:19
  • If this works, then if you don't mind, you can accept the answer as correct by clicking on the checkmack next to the answer. – Onyambu Feb 05 '18 at 19:31
  • @Onyambu - Would you mind showing me the option where the same is? I would really be happy to accept this if you could expand it a bit. – user817995 Feb 19 '18 at 05:29
  • What do you mean by the same is?? I do not understand what you mean. I am so sorry – Onyambu Feb 19 '18 at 23:51