Question

I have a dataset in data.table that has a date range that I've divided into year, month, and day. I'm trying to subset it by month.

Here is the code I'm using to subset

ADVDT6 <- subset(ADVDT, mo %in% 7:12)

My data is here in reproducible format:

ADVDTtest <-
         data.table(structure(list(Issue.Date
         = structure(c(16041,
         16056,
         16067,16011,
         16042, 15859,
         15862, 16021,
         16023, 16011),
         class =
         "Date"),Action
         = structure(c(4L,
         4L, 1L, 1L,
         4L, 2L, 2L,
         1L, 3L,1L),
         .Label =
         c("Internal
         Complaint",
         "Make Good",
         "Make Good -
         Prod","Nothing
         Given", "Pending"), class = "factor"),
         yr =
         c("2013","2013",
         "2013", "2013",
         "2013", "2013",
         "2013", "2013", "2013","2013"),
         mo = c("12",
         "12", "12",
         "11", "12", "06", "06","11", "11", "11"),
         da = c("02",
         "17", "28",
         "02", "03",
         "03","06",
         "12", "14",
         "02")), .Names
         = c("Issue.Date",
         "Action","yr",
         "mo", "da"),
         class =
         c("data.table",
         "data.frame"),
         row.names = c(NA,-10L)))

My issue is that when I subset using my code, I only receive an output from months 10-12 instead of 7-12.

My end goal is to be able to use ggplot2 to plot a bar chart that ignores the NA issues I was having with native barplot. However, I can't get the full subset of data that I want to be plotted!

Thanks a bunch!

Was it helpful?

Solution

ADVDT6 <- subset(ADVDT, mo %in% 7:12) should also be correct. For me it works with a subset of your data that Chargaff used:

    > subset(dd, mo %in% 11:12)
            Issue.Date    Action   yr mo da
    12/2/13     Nothing     Given 2013 12  2
    12/17/13    Nothing     Given 2013 12 17
    12/28/13   Internal Complaint 2013 12 28
    11/2/13    Internal Complaint 2013 11  2
    12/3/13     Nothing     Given 2013 12  3

    > subset(dd, mo %in% 6:11)
            Issue.Date    Action   yr mo da
    11/2/13   Internal Complaint 2013 11  2
    6/3/13        Make      Good 2013  6  3

Maybe you should check if your whole data.frame looks like dd.

OTHER TIPS

I think this does what you are after. Sample of your data :

dd = read.table(textConnection("Issue.Date Action yr mo da
12/2/13 Nothing Given   2013    12      2
12/17/13        Nothing Given   2013    12      17
12/28/13        Internal Complaint      2013    12      28
11/2/13 Internal Complaint      2013    11      2
12/3/13 Nothing Given   2013    12      3
6/3/13  Make Good       2013    6       3"),header=T)

Select in your data frame mo between 7 and 12

dd[which(dd$mo>6&dd$mo<13),]

Results

> dd[which(dd$mo>6&dd$mo<13),]
         Issue.Date    Action   yr mo da
12/2/13     Nothing     Given 2013 12  2
12/17/13    Nothing     Given 2013 12 17
12/28/13   Internal Complaint 2013 12 28
11/2/13    Internal Complaint 2013 11  2
12/3/13     Nothing     Given 2013 12  3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top