【发布时间】:2020-07-05 04:54:11
【问题描述】:
我目前正在尝试对我的数据集中的数据进行子集化。我使用了dput 和str,所以你可以看到我正在使用什么:
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 ...
如您所见,我的日期格式为“DD MM YY 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,希望能得到一些帮助!
【问题讨论】:
-
Phil 一旦您将数据转换为日期,假设它被正确解释,那么您还需要使用标准 iso 日期格式的过滤条件。那是 "2009-12-31 23:00:00" 。对数据进行子集化的更简单方法是使用 grepl 函数。 grepl 在字符串中查找字符串,例如“2009”。所以
my_list_object[grepl("2009", my_list_object)]会让你得到你想要的。