[R] grouping-R-help

P Ehlers ehlers at math.ucalgary.ca
Mon Dec 26 11:42:46 CET 2005


Subhabrata wrote:
> Hello R-experts,
> 
> I have a set of data as follows:
> 
>     age time
> 1   28    1
> 2   53    2
> 3   53    3
> 4   36    4
> 5   54    4
> 6   46    4
> 7   45    5
> 8   31    6
> 9   53    7
> 10  35    7
> 11  62    8
> 12  19    8
> 13  43    2
> 14  51    3
> 15  45    0
> 16  48    2
> 17  49    3
> 18  57    2
> 19  45    3
> 20  27   10
> 21  33   12
> 22  29   14
> 23  46   16
> 24  45   19
> 25  43    2
> 26  28    6
> 27  50    7
> 28  28    4
> 29  33    5
> 30  56    6
> 31  52    6
> 32  50    7
> 
> 
> I want to group the data.
> 
> Where the age coloum will be grouped in to 0-15, 15-20 ...
> and the sum of time of that group will be added to that group
> instead of count of number with in that group.
> 

Let's call your data frame 'dat'.

dat$age.fac <- with(dat, cut(age, breaks = seq(10, 70, by = 10)))
sumtime <- with(dat, tapply(time, age.fac, sum))
newdat <- data.frame(agecat = names(sumtime), sumtime)

Adjust 'breaks' to suit.

Is that what you want?

Peter Ehlers


> 
> Thank you for any help
> 
> With Regards
> Subhabrata Pal
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list