将数据框设置为指定的月份和年份 [英] Subsetting a dataframe for a specified month and year

查看:89
本文介绍了将数据框设置为指定的月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中第一列是d / m / y格式的日期,第二列是数值(销售)。



我想为一年的每个月创建子集(例如11 / 11、12 / 11等)。我尝试了此答案中建议的代码:子集具有多个条件的data.frame



,并且在施加月份条件时有效:

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

,但是当我添加年份条件时,它返回带有错误消息无效的'x'参数的空子集:

 子集(销售,format.Date(日期,%m)== 11& format.Date(日期,%y)== 11 )

我在Ubuntu 10.04上使用R 2.10.1-2,感谢您的帮助。

解决方案

由于您没有提供数据集,因此我通过提供的链接创建了自己的数据集。您的方法对我有用,并且仅当我不满足提供的两个条件(月和年)时,我才获得一个空数据集,所以我猜您正在尝试对日期序列(月和年)进行子集化)不存在(但没有您正在使用的代码就无法确定)。这是我使用的代码: 2006年5月7日,
9104堪萨斯州KS2000111洪堡,阿特拉津市2006年7月23日,
9212堪萨斯州KS2000111洪堡,阿特拉津城0.33 02/11/2007
2094堪萨斯州KS2000111洪堡,阿特拉津市1.4 05/06/2007
16763堪萨斯州KS2000111洪堡市,阿特拉津市0.61 05/11/2009
1076堪萨斯州KS2000111洪堡市,METOLACHLOR市0.48 05/12/2002
1077堪萨斯州KS2000111洪堡,密苏里州城市0.3 05/07/2006)

sales $ V9 <-as.Date(sales $ V9,%m /%d /%Y)
names(sales)[9]<-'date'
子集(sales,format.Date(date,%m)== 05& format.Date(date,%y )== 07)
#V1 V2 V3 V4 V5 V6 V7 V8日期
#4 2094堪萨斯州KS2000111洪堡,阿特拉津1.4 2007年5月6日

子集(销售,格式。日期(日期,%m)== 05和格式。日期e(date,%y)= 10)
#[1] V1 V2 V3 V4 V5 V6 V7 V8 date
#< 0行> (或长度为0的row.names)


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.

解决方案

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)

这篇关于将数据框设置为指定的月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆