2014-02-05 113 views
8

兩個日期我有進口R.我用下面的命令導入製表符分隔文本文件:比較R中

data = read.table(soubor, header = TRUE, sep = "\t", dec = ".", colClasses =c("numeric","numeric","character","Date","numeric","numeric")) 

當我運行str(data)檢查數據類型的我列我得到:

'data.frame': 211931 obs. of 6 variables: 
$ DataValue : num 0 0 0 0 0 0 0 0 0 NA ... 
$ SiteID  : num 1 1 1 1 1 1 1 1 1 1 ... 
$ VariableCode: chr "Sucho" "Sucho" "Sucho" "Sucho" ... 
$ DateTimeUTC : Date, format: "2012-07-01" "2012-07-02" "2012-07-03" "2012-07-04" ... 
$ Latitude : num 50.8 50.8 50.8 50.8 50.8 ... 
$ Longitude : num 15.6 15.6 15.6 15.6 15.6 ... 

前20行我的數據的一個重複的樣品是在這裏:

my_sample = dput(數據[1:20,])

structure(list(DataValue = c(0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, 
NA, NA, NA, NA, NA, NA, 0, 0, 0), SiteID = c(1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), VariableCode = c("Sucho", 
"Sucho", "Sucho", "Sucho", "Sucho", "Sucho", "Sucho", "Sucho", 
"Sucho", "Sucho", "Sucho", "Sucho", "Sucho", "Sucho", "Sucho", 
"Sucho", "Sucho", "Sucho", "Sucho", "Sucho"), DateTimeUTC = structure(c(15522, 
15523, 15524, 15525, 15526, 15527, 15528, 15529, 15530, 15531, 
15532, 15533, 15534, 15535, 15536, 15537, 15538, 15539, 15540, 
15541), class = "Date"), Latitude = c(50.77, 50.77, 50.77, 50.77, 
50.77, 50.77, 50.77, 50.77, 50.77, 50.77, 50.77, 50.77, 50.77, 
50.77, 50.77, 50.77, 50.77, 50.77, 50.77, 50.77), Longitude = c(15.55, 
15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 
15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 15.55, 
15.55)), .Names = c("DataValue", "SiteID", "VariableCode", "DateTimeUTC", 
"Latitude", "Longitude"), row.names = c(NA, 20L), class = "data.frame") 

現在我想通過日期來過濾我的表。請注意,我在for循環內運行我的代碼。首先,我在2012年7月1日之前對數據進行分類並做一些處理。然後,我通過7月2日我的子集數據,並做一些處理,等等。例如,我想帶日期等於2012年7月6日的所有行我試過代碼:

startDate = as.Date("2012-07-01"); 
endDate = as.Date("2012-07-20"); 
all_dates = seq(startDate, endDate, 1); 

#the following code I'm trying to run inside a loop... 
for (j in 1:length(all_dates)) { 
    filterdate = all_dates[j]; 
    my_subset = my_sample[my_sample$DateTimeUTC == filterdate,] 
    #now I want do do some processing on my_subset... 
} 

但上面的代碼返回從循環的第7步開始的空數據集。

因此,舉例來說:

subset_one = my_sample[my_sample$DateTimeUTC == all_dates[6],] 

回報:3 obs of 6 variables

但是,對於一些未知的原因,例如:

subset_two = my_sample[my_sample$DateTimeUTC == all_dates[7],] 

回報:0 obs of 6 variables

(注意:我編輯了上面的代碼,使我的問題,100%可重複)

任何想法我做錯了嗎?

+1

'數據[$ DateTimeUTC == as.Date(「2012-07-04 「),]'請閱讀R的介紹來學習基本子集。 – Roland

+0

@Roland:你的例子工作並返回所需的結果,但由於某種原因,當我以編程方式設置過濾日期(例如在for循環內)時,我得到一個空數據集。我編輯了我的示例代碼來澄清我的問題。 – jirikadlec2

+0

沒有一個可重複的例子,我無法幫助你。然而,我敢打賭我的午餐,你不需要'for'循環,不應該使用一個。 – Roland

回答

5

以下解決方案解決了我的問題: 而不是使用Date數據類型,我嘗試使用POSIXct數據類型。 下面是用於讀取製表符分隔文本文件的子集之後,在我的for循環的所有步驟,工作的示例代碼:

data = read.table("data.txt", header = TRUE, sep = "\t", dec = ".", 
    colClasses =c("numeric","numeric","character","POSIXct","numeric","numeric")); 
startDate = as.POSIXct("2012-07-01"); 
endDate = as.POSIXct("2012-07-20"); 
all_dates = seq(startDate, endDate, 86400); #86400 is num of seconds in a day 

#the following code I'm trying to run inside a loop... 
for (j in 1:length(all_dates)) { 
    filterdate = all_dates[j]; 
    my_subset = data[data$DateTimeUTC == filterdate,] 
    #now I want do do some processing on my_subset... 
} 
+0

我使用了類似的方法。我用'subset'命令替換循環,所以我使用子集(數據,數據$ DateTimeUTC%在%all_dates中)。在我的情況下,我需要用'as.Date'函數包裝數據 - subset(data,as.Date(data $ DateTimeUTC)%in%as.Date(all_dates)) – 32cupo