r语言 - 日期格式为 "01.01.2009 08:00:00, 01.01.2009 9:00:00, ..." 的子集数据



我目前正在尝试从数据集中对数据进行子集设置。我使用了dputstr,所以你可以看到我在使用什么:

dput:

structure(list(Date = structure(1:10, .Label = c("01.01.2009 00:00:00", 
"01.01.2009 01:00:00", "01.01.2009 02:00:00", "01.01.2009 03:00:00", 
"01.01.2009 04:00:00", "01.01.2009 05:00:00", "01.01.2009 06:00:00", 
"01.01.2009 07:00:00", "01.01.2009 08:00:00", "01.01.2009 09:00:00"
), class = "factor"), SWC = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN)), row.names = c(NA, 10L), class = "data.frame")

str:

'data.frame':   8756 obs. of  2 variables:
$ Date: Factor w/ 96408 levels "01.01.2009 00:00:00",..: 1 2 3 4 5 6 7 8 9 10 ...
$ SWC : num  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...

正如你所看到的,我的日期格式是"年月日HH MM SS"。为了给我的数据子集,我尝试使用subset()函数(见下文(。

Mois2009_2 <- subset(Mois1$Date <= "31.12.2009 23:00:00") 

但我得到以下错误:

In Ops.factor(Mois1$Date, "31.12.2009 23:00:00") :
‘<=’ not meaningful for factors

我查找了错误,发现我必须使用转换日期

as.Date(Mois1$Date)

这产生了另一个错误,上面写着:

Character string is not in a unique standard format

我刚开始使用R,所以我很感激您的帮助!

您可以按如下方式解决它:

Mois1 <- structure(list(Date = structure(1:10, .Label = c("01.01.2009 00:00:00", 
"01.01.2009 01:00:00", "01.01.2009 02:00:00", "01.01.2009 03:00:00", 
"01.01.2009 04:00:00", "01.01.2009 05:00:00", "01.01.2009 06:00:00", 
"01.01.2009 07:00:00", "01.01.2009 08:00:00", "01.01.2009 09:00:00"
), class = "factor"), SWC = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN)), row.names = c(NA, 10L), class = "data.frame")
Mois1$Date <- as.Date(Mois1$Date, format = "%d.%m.%Y %H:%M:%S")
Mois2009_2 <- subset(Mois1, Date <= "2009-12-31 23:00:00")

我们可以从dplyr使用filter

library(dplyr)
library(lubridate)
df1 %>%
filter(dmy_hms(Date) <= "2009-12-31 23:00:00")

最新更新