我有一个以制表符分隔的文本文件导入到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%可重复)
任何想法我做错了什么?
答案 0 :(得分: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...
}