-1

If I have a data set and want to sum a specific column on the basis of the specific date range in r? for example Dataset

Guest  Sales  date
A        50      2018-08-10
B        180     2018-08-10
C         20     2018-08-15
D         390    2018-08-20

And I want the sum of sales between 2018-08-10 and 2018-08-16 ?

zx8754
  • 52,746
  • 12
  • 114
  • 209
ruchi
  • 1
  • 1
  • 1
    Please keep your data as text not image. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – zx8754 Dec 11 '18 at 10:21

2 Answers2

1

You could try:

sum(df[df$date >="2018-08-10" & df$date<="2018-08-16",]$Sales)

or

sum(subset(df, date>="2018-08-10" & date<="2018-08-16")$Sales)

For reproductivity:

df <- data.table::fread("Guest  Sales  date
                         A        50      2018-08-10
                         B        180     2018-08-10
                         C         20     2018-08-15
                         D         390    2018-08-20")
Maylo
  • 572
  • 5
  • 16
  • But, I need the sum of sales in the duration of 2018-08-10 and 2018-08-16 . which should be 250 not 20 – ruchi Dec 18 '18 at 08:47
  • this is as simple as adding `>=` and `<=` instead of only `>` and `<`. I've edited my answer to suit your needs – Maylo Dec 18 '18 at 08:51
0

A solution with the library dplyr:

library(dplyr)
df %>% 
  filter(date > "2018-08-10" & date < "2018-08-16") %>% 
  summarise(sum_sales = sum(Sales))
#    sum_sales
# 1        20

Data:

df <- data.table::fread("Guest  Sales  date
                         A        50      2018-08-10
                        B        180     2018-08-10
                        C         20     2018-08-15
                        D         390    2018-08-20", data.table=FALSE)
RLave
  • 8,144
  • 3
  • 21
  • 37