[R] Sorting data.frame datewise in a descending order and geting datewise subtotl

arun smartpink111 at yahoo.com
Wed Jul 2 15:31:36 CEST 2014


Hi veepsirtt,


If `dat` is the dataset

library(dplyr)
 dat %>% 

group_by(DATE) %>% 

summarize(PROFIT=sum(PROFIT)) %>%
 arrange(desc(as.Date(DATE,format="%d/%m/%Y")))
Source: local data frame [4 x 2]

        DATE PROFIT
1 02/07/2014  -1350
2 01/07/2014   9400
3 30/06/2014  11325
4 27/06/2014   6850


If you just wanted to have a new variable subTotalPROFIT and not summarize the dataset


 dat %>% 

 group_by(DATE) %>% 

 mutate(subTotalPROFIT=sum(PROFIT)) %>% 

arrange(desc(as.Date(DATE,format="%d/%m/%Y")))

A.K.



On Wednesday, July 2, 2014 4:03 AM, Velappan Periasamy <veepsirtt at gmail.com> wrote:



Hi A.K,
How to Sort the  given data.frame date wise in a descending order
 and getting date wise subtotal

SLNO.        DATE        SCRIP        PROFIT 
6006302/07/2014Aluminium       -1000
6005702/07/2014Copper  900
6005602/07/2014LEAD       -1250
6002901/07/2014Crude Oil6400
6003401/07/2014LEAD        1500
6002501/07/2014Nickel        1500
5998030/06/2014Nickel          475
5998430/06/2014Natural Gas3000
5997230/06/2014Crude Oil2600
5997130/06/2014Copper3750
5997030/06/2014Natural Gas1500
5992427/06/2014Aluminium    500
5992227/06/2014LEAD        2250
5992027/06/2014Copper1100
5991827/06/2014Natural Gas3000

Thanks
veepsirtt



More information about the R-help mailing list