-1

I have a dataframe where the second column is a date in m/d/y format. There are 4 main column: name, date, time_taken, homequeue

I want to create subsets for particular month of one year (eg. 11/11, 12/11 etc)which i will use to calculate the total timetaken by people of particular homequeue who work from 4/1/2020 to 4/7/2020

I tried the code suggested in this answer: subset a data.frame with multiple conditions (Subsetting a dataframe for a specified month and year)

and it is not working for me. Please check image for dataset: utilization data **

df <- read.csv('Rm_2020-04-20 copy.csv')

combo <- subset(df, home_queue == 'Brand Effects', format.Date(date, "%m") == "04" & format.Date(date, "%Y") == "20")

**

but it returns an empty subset with error message

Error in as.POSIXlt.numeric(x) : 'origin' must be supplied

I am using R version 3.3.3 (2017-03-06) on Mac , thanks in adavance. I am new to the stackoverflow and learning R.

  • Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 23 '20 at 08:11

1 Answers1

0

The OP is relatively unclear, so this answer must be speculative. But it appears that what is missing is the conversion of your date values to POSIXct values. This conversion can be achieved thus:

Let's assume you have data similar to this:

my.data <- read.csv(text = '
          Date,      Var2,  Var3
                    05/10/2011,    AK,     aa
                    06/15/2011,    AK,     bb
                    07/21/2011,    OH,     cc
                    NA,    OH,     dd
                    05/13/2012,    PA,     ee
                    07/22/2012,    AL,     ff
                    03/28/2013,    NY,     gg
                    ', header=TRUE, stringsAsFactors = FALSE, na.strings = 'NA', strip.white = TRUE)

At this point your dates are, for R, just character strings, as can be seen from calling str:

str(my.data)
'data.frame':   7 obs. of  3 variables:
 $ Date: chr  "05/10/2011" "06/15/2011" "07/21/2011" NA ...
 $ Var2: chr  "AK" "AK" "OH" "OH" ...
 $ Var3: chr  "aa" "bb" "cc" "dd" ...

To make R recognize the dates as dates proper, use as.POSIXct:

my.data$my_Date <- as.POSIXct(my.data$Date, format = "%m/%d/%Y")

Now you can subset, for example, on May:

my.data[format.Date(my.data$my_Date, "%m")=="05" &
      !is.na(my.data$my_Date),]

        Date Var2 Var3    my_Date
1 05/10/2011   AK   aa 2011-05-10
5 05/13/2012   PA   ee 2012-05-13

Or, select May of one particular year, e.g., 2012:

my.data[format.Date(my.data$my_Date, "%Y")=="2012" & 
          format.Date(my.data$my_Date, "%m")=="05" &
          !is.na(my.data$my_Date),]

        Date Var2 Var3    my_Date
5 05/13/2012   PA   ee 2012-05-13
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34