在R中选择特定日期 [英] Selecting Specific Dates in R

查看:273
本文介绍了在R中选择特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据日期列表而不是日期范围在R中创建数据子集。

I am wondering how to create a subset of data in R based on a list of dates, rather than by a date range.

例如,我有以下数据集 data 包含3年的6分钟数据。

For example, I have the following data set data which contains 3 years of 6-minute data.

      date            zone month day year hour minute temp speed gust dir 
1  09/06/2009 00:00   PDT     9   6 2009    0      0   62    2   15 156 
2  09/06/2009 00:06   PDT     9   6 2009    0      6   62   13   16 157 

我使用了 breeze< -subset(data,ws> = 15& wd> = 247.5& wd< = 315,select = date:dir)选择满足我的海风条件的行,这很好,但是我要做的是创建包含符合我的条件的那些日子的子集。

I have used breeze<-subset(data, ws>=15 & wd>=247.5 & wd<=315, select=date:dir) to select the rows which meet my criteria for a sea breeze, which is fine, but what I want to do is create a subset of the days which contain those times that meet my criteria.

我用过...

as.character(breeze$date)
trimdate<-strtrim(breeze$date, 10)
breezedate<-as.Date(trimdate, "%m/%d/%Y")
breezedate<-format(breezedate, format="%m/%d/%Y")

...以提取满足我条件的每一行中的日期,因此我有一个名为 Breezeedate 的变量,其中包含我想要的日期列表(不是最精明的代码,我可以)。列表中大约有两百个日期。我要使用下一条命令尝试的操作是在原始数据集 data 中创建一个子集,该子集仅包含满足海风条件的日期,而不仅仅是特定时间。

...to extract the dates from each row that meets my criteria so I have a variable called breezedate that contains a list of the dates that I want (not the most eloquent coding to do this, I'm sure). There are about two-hundred dates in the list. What I am trying to do with the next command is in my original dataset data to create a subset which contains only those days which meet the seabreeze criteria, not just the specific times.

breezedays<-(data$date==breezedate)

我认为这里的问题之一是我正在将一个值与一个值列表进行比较,但是我不确定如何使它起作用。

I think one of my issues here is that I am comparing one value to a list of values, but I am not sure how to make it work.

推荐答案

假设您的微风列表看起来像这样,data $ date是简单的字符串:

Lets assume your breezedate list looks like this and data$date is simple string:

  breezedate <- as.Date(c("2009-09-06", "2009-10-01"))

这可能是您想要的:

  breezedays <- data[as.Date(data$date, '%m/%d/%Y') %in% breezedate]

这篇关于在R中选择特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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