2015-05-19 19 views
1

我是R的新手,所以这可能是一个简单的问题,但它给我带来了很多困难。在R中的多个数据帧中设置日期和时间

我想跨数据框中找到的两个值之间的子集,我试图在这两个值之间的子集时遇到困难。我将首先描述我已经完成的工作,正在工作的工作,然后是什么不工作。

我有两个数据帧。一个有一系列风暴数据,包括风暴事件的日期,另一个有一系列数据,对应于成千上万次监测事件的排放。我试图查看是否有任何排放数据在风暴事件开始和结束日期/时间内相对应。

我所做的迄今如下:

例出院数据:

X. DateTime  Depth DateTime1   newcol 
1 3 8/2/2013 13:15 0.038 2013-08-02 13:15:00 1375463700 
2 4 8/2/2013 13:30 0.038 2013-08-02 13:30:00 1375464600 
3 5 8/2/2013 13:45 0.039 2013-08-02 13:45:00 1375465500 
4 6 8/2/2013 14:00 0.039 2013-08-02 14:00:00 1375466400 

例风暴数据:

Storm newStart newEnd 
1 1 1382125500 1382130000 
2 2 1385768100 1385794200 

#Make a value to which the csv files are attached 
CA_Storms <- read.csv(file = "CA_Storms.csv", header = TRUE, stringsAsFactors = FALSE) 
CA_adj <- read.csv(file = "CA_Adj.csv", header = TRUE, stringsAsFactors = FALSE) 

#strptime function (do this for all data sets) 
CA_adj$DateTime1 <- strptime(CA_adj$DateTime, format = "%m/%d/%Y %H:%M") 
CA_Storms$Start.time1 <- strptime(CA_Storms$Start.time, format = "%m/%d/%Y %H:%M") 
CA_Storms$End.time1 <- strptime(CA_Storms$End.time, format = "%m/%d/%Y %H:%M") 

#Make dates and times continuous 
CA_adj$newcol <- as.numeric(CA_adj$DateTime1) 
CA_Storms$newStart <- as.numeric(CA_Storms$Start.time1) 
CA_Storms$newEnd <- as.numeric(CA_Storms$End.time1) 

这让我成功地做到以下子集:

CA_adj[CA_adj$newcol == "1375463700", ] 

Example output: 
X.  DateTime Depth   DateTime1  newcol 
    1 3 8/2/2013 13:15 0.038 2013-08-02 13:15:00 1375463700 

CA_adj[CA_adj$newcol == CA_Storms[1,19], ] 

X.  DateTime   Depth DateTime1   newcol 
7403 7408 10/18/2013 15:45 0.058 2013-10-18 15:45:00 1382125500 

CA_adj[CA_adj$newcol <= CA_Storms[1,20], ] 

然而,每当我试图把它的两个值,如之间移动:

CA_adj[CA_adj$newcol >= CA_Storms[1,19] & CA_adj$newol <= CA_Storms[1,20], ]

其与此回应:

[1] X.  DateTime Depth  DateTime1 newcol 
<0 rows> (or 0-length row.names) 

我知道这个输出是不正确,因为,通过通过我的大型数据集粗略查看,至少存在一个符合这些标准的值。

什么给?

+0

请发布样本数据和期望的输出。 – Soheil

+0

不是'CA_adj [CA_adj $ newcol> = CA_Storms [1,19] | CA_adj $ newol <= CA_Storms [1,20],]'你想要什么? – Robert

+0

@Sheheil谢谢你的建议。我编辑了这篇文章,现在添加了这些内容。 –

回答

0
discharge<-data.frame(x=c(3,4,5,6), 
         DateTime=c("8/2/2013 13:15","8/2/2013 13:30", 
            "8/2/2013 13:45","8/2/2013 14:00"), 
         Depth=c(0.038, 0.038, 0.039, 0.039) 
        ) 
discharge$DateTime1<- as.POSIXct(discharge$DateTime, format = "%m/%d/%Y %H:%M") 

storm<-data.frame(storm=c(1,2), 
        start=c("8/2/2013 13:15","8/2/2013 16:30"), 
        end=c("8/2/2013 13:45","8/2/2013 16:45") 
       ) 

storm$start<- as.POSIXct(storm$start, format = "%m/%d/%Y %H:%M") 
storm$end<- as.POSIXct(storm$end, format = "%m/%d/%Y %H:%M") 


discharge[(discharge$DateTime1>=storm[1,2] & discharge$DateTime1<=storm[1,3]),]