aggregate - Merging output in R -


max=aggregate(cbind(a$value,date=a$date) ~ format(a$date, "%m") + cut(a$class, breaks=c(0,2,4,6,8,10,12,14)) , data = a, max)[-1] max$date=as.date(max$date, origin = "1970-01-01") 

sample data :

date         grade    value 2008-09-01     1        20 2008-09-02     2        30 2008-09-03     3        50     .     . 2008-09-30     2        75     .     . 2008-10-01     1        95     .     . 2008-11-01     4        90     .     .  2008-12-01     1        70 2008-12-02     2        40 2008-12-28     4        30 2008-12-29     1        40 2008-12-31     3        50 

my expected output according above table first month :

 date         grade    value 2008-09-30    (0,2]     75 2008-09-02    (2,4]     50 

output in real data :

                format(date, "%m") 1                        09 2                        10 3                        11 4                        12 5                        09 6                        10 7                        11      cut(a$grade, breaks = c(0, 2, 4, 6, 8, 10, 12, 14))        value 1                                                        (0,2] 0.30844444 2                                                        (0,2] 1.00000000 3                                                        (0,2] 1.00000000 4                                                        (0,2] 0.73333333 5                                                        (2,4] 0.16983488 6                                                        (2,4] 0.09368000 7                                                        (2,4] 0.10589335            date 1  2008-09-30 2  2008-10-31 3  2008-11-28 4  2008-12-31 5  2008-09-30 6  2008-10-31 7  2008-11-28 

the output not according sample data , data big . simple logic there grades 1 10 , want find highest value month in corresponding grade groups . eg : need highest value each group (0,2],(0,4] etc

i used aggregate condition function max , 2 grouping 2 columns date , grade . when run code , display value of max , 3 tables output 1 after other. want plot output not able because of .so how can merge these output ?

try:

 library(dplyr)  %>%  group_by(month=format(date, "%m"), grade=cut(grade, breaks=seq(0,14,by=2))) %>%  summarise_each(funs(max))   #  month grade       date value  #1    09 (0,2] 2008-09-30    75  #2    09 (2,4] 2008-09-03    50  #3    10 (0,2] 2008-10-01    95  #4    11 (2,4] 2008-11-01    90  #5    12 (0,2] 2008-12-29    70  #6    12 (2,4] 2008-12-31    50 

or using data.table

 library(data.table)   setdt(a)[, list(date=max(date), value=max(value)),                           by= list(month=format(date, "%m"),                      grade=cut(grade, breaks=seq(0,14, by=2)))]   #       month grade       date value   #1:    09 (0,2] 2008-09-30    75   #2:    09 (2,4] 2008-09-03    50   #3:    10 (0,2] 2008-10-01    95   #4:    11 (2,4] 2008-11-01    90   #5:    12 (0,2] 2008-12-29    70   #6:    12 (2,4] 2008-12-31    50 

or using aggregate

  res <- transform(with(a,             aggregate(cbind(value, date),               list(month=format(date, "%m") ,grade=cut(grade, breaks=seq(0,14, by=2))), max)),            date=as.date(date, origin="1970-01-01"))    res[order(res$month),]   # month grade value       date   #1    09 (0,2]    75 2008-09-30   #4    09 (2,4]    50 2008-09-03   #2    10 (0,2]    95 2008-10-01   #5    11 (2,4]    90 2008-11-01   #3    12 (0,2]    70 2008-12-29   #6    12 (2,4]    50 2008-12-31 

data

 <-  structure(list(date = structure(c(14123, 14124, 14125, 14152,     14153, 14184, 14214, 14215, 14241, 14242, 14244), class = "date"),     grade = c(1l, 2l, 3l, 2l, 1l, 4l, 1l, 2l, 4l, 1l, 3l), value = c(20l,     30l, 50l, 75l, 95l, 90l, 70l, 40l, 30l, 40l, 50l)), .names = c("date",    "grade", "value"), row.names = c(na, -11l), class = "data.frame") 

update

if want include year in grouping

   library(dplyr)    %>%     group_by(month=format(date, "%m"), year=format(date, "%y"), grade=cut(grade, breaks=seq(0,14, by=2)))%>%   summarise_each(funs(max))   #   month year grade       date value   #1     09 2008 (0,2] 2008-09-30    75   #2     09 2008 (2,4] 2008-09-03    50   #3     09 2009 (0,2] 2009-09-30    75   #4     09 2009 (2,4] 2009-09-03    50   #5     10 2008 (0,2] 2008-10-01    95   #6     10 2009 (0,2] 2009-10-01    95   #7     11 2008 (2,4] 2008-11-01    90   #8     11 2009 (2,4] 2009-11-01    90   #9     12 2008 (0,2] 2008-12-29    70   #10    12 2008 (2,4] 2008-12-31    50   #11    12 2009 (0,2] 2009-12-29    70   #12    12 2009 (2,4] 2009-12-31    50 

data

 <- structure(list(date = structure(c(14123, 14124, 14125, 14152,     14153, 14184, 14214, 14215, 14241, 14242, 14244, 14488, 14489,    14490, 14517, 14518, 14549, 14579, 14580, 14606, 14607, 14609   ), class = "date"), grade = c(1l, 2l, 3l, 2l, 1l, 4l, 1l, 2l,    4l, 1l, 3l, 1l, 2l, 3l, 2l, 1l, 4l, 1l, 2l, 4l, 1l, 3l), value = c(20l,    30l, 50l, 75l, 95l, 90l, 70l, 40l, 30l, 40l, 50l, 20l, 30l, 50l,    75l, 95l, 90l, 70l, 40l, 30l, 40l, 50l)), .names = c("date",    "grade", "value"), row.names = c("1", "2", "3", "4", "5", "6",    "7", "8", "9", "10", "11", "12", "21", "31", "41", "51", "61",     "71", "81", "91", "101", "111"), class = "data.frame") 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -