数据表列中每年最低日期的行号

问题描述 投票:0回答:1

我有一个数据表,其中有一列包含日期:

> dat
          Var1  Var2
        <Date> <num>
 1: 2023-11-01 18.05
 2: 2023-12-01  4.65
 3: 2024-01-01 20.34
 4: 2024-02-01 21.71
 5: 2024-03-01 51.60
 6: 2024-04-01 55.54
 7: 2024-05-01 50.34
 8: 2024-06-01 45.90
 9: 2024-07-01 21.26
10: 2024-08-01 19.52

> dput(dat)
structure(list(Var1 = structure(c(19662, 19692, 19723, 19754, 
19783, 19814, 19844, 19875, 19905, 19936), class = "Date"), Var2 = c(18.05, 
4.65, 20.34, 21.71, 51.6, 55.54, 50.34, 45.9, 21.26, 19.52)), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"))

我想要一个包含每年最低日期的行的行号向量,在本例中:

> first.dates
[1] 1 3

我怎样才能找到这些?

r dataframe date datatable
1个回答
0
投票

也许这会有所帮助

> which(dat[, order(Var1), year(Var1)]$V1 == 1)
[1] 1 3
© www.soinside.com 2019 - 2024. All rights reserved.