Subsetting a dataframe for a specified month and year

廉价感情. 提交于 2019-12-17 19:45:19

问题


I have a dataframe where the first column is a date in d/m/y format and the second is a numeric value (sales).

I want to create subsets for each month of one year (eg. 11/11, 12/11 etc). I tried the code suggested in this answer: subset a data.frame with multiple conditions

and it works when the condition on the month is imposed:

subset(sales, format.Date(date, "%m")=="11")

but it returns an empty subset with error message invalid 'x' argument when I add the year condition:

subset(sales, format.Date(date, "%m")=="11" & format.Date(date, "%y")=="11")

I am using R 2.10.1-2 on Ubuntu 10.04, thanks for the help you can give.


回答1:


Since you didn't provide a data set I made my own from the link you provided. Your method works for me and I get an empty data set only if I don't meet both of the conditions supplied (month and year) so I'm guessing you're you're attempting to subset a date series (month and year) that doesn't exist (but can't tell for certain without the code you're using). Here's the code I used:

sales <- read.table(text="2372  Kansas KS2000111 HUMBOLDT, CITY OF    ATRAZINE    1.3 05/07/2006
9104  Kansas KS2000111 HUMBOLDT, CITY OF    ATRAZINE   0.34 07/23/2006
9212  Kansas KS2000111 HUMBOLDT, CITY OF    ATRAZINE   0.33 02/11/2007
2094  Kansas KS2000111 HUMBOLDT, CITY OF    ATRAZINE    1.4 05/06/2007
16763 Kansas KS2000111 HUMBOLDT, CITY OF    ATRAZINE   0.61 05/11/2009
1076  Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR   0.48 05/12/2002
1077  Kansas KS2000111 HUMBOLDT, CITY OF METOLACHLOR    0.3 05/07/2006")

sales$V9 <- as.Date(sales$V9, "%m/%d/%Y")
names(sales)[9] <- 'date'
subset(sales, format.Date(date, "%m")=="05" & format.Date(date, "%y")=="07")
#    V1     V2        V3        V4   V5 V6       V7  V8       date
#4 2094 Kansas KS2000111 HUMBOLDT, CITY OF ATRAZINE 1.4 2007-05-06

subset(sales, format.Date(date, "%m")=="05" & format.Date(date, "%y")=="10")
#[1] V1   V2   V3   V4   V5   V6   V7   V8   date
#<0 rows> (or 0-length row.names)



回答2:


This answer avoids subset, handles missing observations and uses as.POSIXct date/time format. Although, the rest of the code is virtually the same as in Tyler Rinker's answer. Note that I have to specify the name of the date/time variable inside as.POSIXct rather than using the name of the unformatted variable Date_Time.

my.data <- read.csv(text = '
          Date_Time,      state,  city
    10/05/2011 07:32:40,    AK,     aa
    15/06/2011 13:26:02,    AK,     bb
    19/07/2011 13:26:02,    OH,     cc
                     NA,    OH,     dd
    20/05/2012 14:57:27,    PA,     ee
    22/07/2012 14:57:27,    AL,     ff
    20/03/2013 15:03:18,    NY,     gg
', header=TRUE, stringsAsFactors = FALSE, na.strings = 'NA', strip.white = TRUE)

my.data$my_Date_Time <- as.POSIXct(my.data$Date_Time, format = "%d/%m/%Y %H:%M:%S")

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

#             Date_Time state city        my_Date_Time
# 1 10/05/2011 07:32:40    AK   aa 2011-05-10 07:32:40
# 5 20/05/2012 14:57:27    PA   ee 2012-05-20 14:57:27


# Select 2012
my.data[format.Date(my.data$my_Date_Time, "%Y")=="2012" &
        !is.na(my.data$my_Date_Time),]

#             Date_Time state city        my_Date_Time
# 5 20/05/2012 14:57:27    PA   ee 2012-05-20 14:57:27
# 6 22/07/2012 14:57:27    AL   ff 2012-07-22 14:57:27

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

#             Date_Time state city        my_Date_Time
# 5 20/05/2012 14:57:27    PA   ee 2012-05-20 14:57:27



回答3:


The "Y" is case sensitive in dates. I don't know why, but the "m" for months and the "d" for days are lower case, but the "Y" must be upper case. This should work for you:

subset(sales, format.Date(date, "%m")=="11" & format.Date(date, "%Y")=="11")


来源:https://stackoverflow.com/questions/9407622/subsetting-a-dataframe-for-a-specified-month-and-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!