从R中的日期对象中提取精确的年份

时间:2013-12-02 10:09:03

标签: r date time

一个简单的问题,但我无法弄清楚。这是我整个df的一部分:

structure(list(ID = structure(c(12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("B0F", 
"B12T", "B1T", "B21T", "B22F", "B26T", "B2F", "B33F", "B3F", 
"B4F", "B7F", "P1", "P21", "P24", "P25", "P27", "P28", "P29"), class = "factor"), 
    Data = structure(c(9646, 9836, 9938, 10043, 10134, 10203, 
    10302, 10354, 10421, 10528, 10680, 10763, 10841, 10913, 11032, 
    11129, 11229, 11289, 11411, 11495, 11572, 11647, 11792, 11850, 
    11975, 12009, 12144, 12229, 12332, 12382, 12499, 12549, 12682, 
    12730, 12858, 12948, 12850, 13122, 13235, 13284, 13383, 13481, 
    13578, 13671, 13760, 13802, 14021, 14097, 14169, 14426, 14531, 
    14792, 14881, 15134, 15209), class = "Date"), T = c(11.3, 
    9.7, 9.8, 10.5, 9.9, 10, 10, 10.1, 10, 10, 10.5, 10.4, 10.6, 
    9.9, 10.1, 10.5, 10.3, 10.4, 10.4, 10.4, 10.7, 10.5, 10.6, 
    10.4, 10.6, 10.6, 10.8, 10.7, 10.4, 10.6, 10.7, 10.7, 10.5, 
    10.5, 10.2, 10.6, 10.9, 10.4, 10.6, 10.8, 10.6, 10.5, 10.7, 
    11.1, 11.1, 11.1, 10.9, 10.9, 10.6, 11, 10.5, 11.2, 10.9, 
    10.7, 11.1), ph = c(6.8, 6.9, 7.1, 6.9, 7, 6.93, 7.01, 6.9, 
    7.01, 6.84, 6.97, 6.88, 6.82, 6.77, 6.9, 7.07, 7.08, 6.96, 
    6.87, 6.87, 6.95, 6.88, 6.78, 6.8, 6.9, 6.91, 7.07, 7.05, 
    6.71, 6.98, 6.95, 6.9, 6.74, 6.71, 6.63, 6.72, 6.78, 6.89, 
    6.91, 6.82, 6.84, 6.98, 6.8, 6.81, 6.76, 6.72, 6.9, 6.91, 
    6.96, 6.98, 6.94, 7.02, 7, 6.92, 6.94), EC = c(1840L, 1060L, 
    940L, 760L, 820L, 1038L, 1035L, 839L, 767L, 433L, 951L, 936L, 
    971L, 939L, 443L, 963L, 950L, 965L, 799L, 971L, 980L, 977L, 
    990L, 980L, 964L, 971L, 949L, 1032L, 920L, 928L, 925L, 931L, 
    1000L, 957L, 856L, 922L, 898L, 882L, 625L, 953L, 838L, 895L, 
    2170L, 2140L, 1499L, 2120L, 881L, 902L, 870L, 541L, 891L, 
    876L, 860L, 868L, 877L), O2 = c(0.1, 0.2, 0.1, 0.9, 0.2, 
    NA, 0.2, 0.1, 0.1, 0.1, 0.4, NA, NA, NA, 0.1, 0.1, 0.1, NA, 
    0.1, 0.1, 0.4, NA, NA, 0.1, 0.1, 0.1, NA, 0.5, 0.2, 0.2, 
    0.3, 0.2, 0.1, 0.1, 0.1, 0.1, 0.2, 0.6, 0.3, 0.6, NA, 0.4, 
    0.4, 0.4, 0.2, 0.3, 0.1, 0.2, 0.1, NA, 0.3, 2.1, NA, 0.3, 
    NA)), .Names = c("ID", "Data", "T", "ph", "EC", "O2"), class = "data.frame", row.names = c(NA, 
55L))

我想要的是在1999年之前提取包含在所有观察中的另一个df。日期列是一个Date对象。

提前致谢

1 个答案:

答案 0 :(得分:2)

这是一个非常简单的问题。你没有说你尝试过什么不行,但显而易见的第一次尝试是:DF[DF$Data < "1999-01-01",]。这可以正常工作,因为<会自动将"1999-01-01"转换为日期。

> df[df$Data < "1999-01-01",]
   ID       Data    T   ph   EC  O2
1  P1 1996-05-30 11.3 6.80 1840 0.1
2  P1 1996-12-06  9.7 6.90 1060 0.2
3  P1 1997-03-18  9.8 7.10  940 0.1
4  P1 1997-07-01 10.5 6.90  760 0.9
5  P1 1997-09-30  9.9 7.00  820 0.2
6  P1 1997-12-08 10.0 6.93 1038  NA
7  P1 1998-03-17 10.0 7.01 1035 0.2
8  P1 1998-05-08 10.1 6.90  839 0.1
9  P1 1998-07-14 10.0 7.01  767 0.1
10 P1 1998-10-29 10.0 6.84  433 0.1