R:定位列唯一值的上次观察索引的最佳方法

R:定位列唯一值的上次观察索引的最佳方法,r,zoo,R,Zoo,我有以下数据。它总是以升序排列的。我希望能够找到所有唯一值的最后一个值,即0、1、2、3、4….的最后一个值。在下面的示例中,1不存在,因此可以跳过并继续查找最后一个值2并返回索引 我想要一个不同唯一值的所有最后观测的指数向量 我该怎么做?谢谢 structure(c(0, 0, 0, 0, 2, 2, 3, 3, 13, 14, 14, 14, 14, 24, 34, 35, 37, 38, 38, 40, 42, 42, 43, 43, 44, 54, 54, 54, 64), i

我有以下数据。它总是以升序排列的。我希望能够找到所有唯一值的最后一个值,即
0、1、2、3、4….
的最后一个值。在下面的示例中,
1
不存在,因此可以跳过并继续查找最后一个值
2
并返回索引

我想要一个不同唯一值的所有最后观测的指数向量

我该怎么做?谢谢

    structure(c(0, 0, 0, 0, 2, 2, 3, 3, 13, 14, 14, 14, 14, 24, 34, 
35, 37, 38, 38, 40, 42, 42, 43, 43, 44, 54, 54, 54, 64), index = structure(c(1167667200, 
1167753600, 1167840000, 1167926400, 1168012800, 1168099200, 1168185600, 
1168272000, 1168358400, 1168444800, 1168531200, 1168617600, 1168704000, 
1168790400, 1168876800, 1168963200, 1169049600, 1169136000, 1169222400, 
1169308800, 1169395200, 1169481600, 1169568000, 1169654400, 1169740800, 
1169827200, 1169913600, 1.17e+09, 1170086400), tzone = "", tclass = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), .Dim = c(29L, 1L), .Dimnames = list(
    NULL, "testing"))
图书馆(动物园)
df
库(动物园)

df您可以使用
rle
函数来确定每个值的运行长度,然后通过
cumsum
索引到适当的行中:

indices <- cumsum(rle(as.vector(a))$lengths)
a[indices]
                    testing
2007-01-04 16:00:00       0
2007-01-06 16:00:00       2
2007-01-08 16:00:00       3
2007-01-09 16:00:00      13
2007-01-13 16:00:00      14
2007-01-14 16:00:00      24
2007-01-15 16:00:00      34
2007-01-16 16:00:00      35
2007-01-17 16:00:00      37
2007-01-19 16:00:00      38
2007-01-20 16:00:00      40
2007-01-22 16:00:00      42
2007-01-24 16:00:00      43
2007-01-25 16:00:00      44
2007-01-28 16:00:00      54
2007-01-29 16:00:00      64

索引您可以使用
rle
函数来确定每个值的运行长度,然后通过
cumsum
将其索引到适当的行中:

indices <- cumsum(rle(as.vector(a))$lengths)
a[indices]
                    testing
2007-01-04 16:00:00       0
2007-01-06 16:00:00       2
2007-01-08 16:00:00       3
2007-01-09 16:00:00      13
2007-01-13 16:00:00      14
2007-01-14 16:00:00      24
2007-01-15 16:00:00      34
2007-01-16 16:00:00      35
2007-01-17 16:00:00      37
2007-01-19 16:00:00      38
2007-01-20 16:00:00      40
2007-01-22 16:00:00      42
2007-01-24 16:00:00      43
2007-01-25 16:00:00      44
2007-01-28 16:00:00      54
2007-01-29 16:00:00      64
索引您可以尝试:

which(rev(!duplicated(rev(df$testing))))
#> [1]  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29
您可以尝试:

which(rev(!duplicated(rev(df$testing))))
#> [1]  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29
1)如果
x
是输入xts对象,则会给出每个元素最后一次出现的索引

findInterval(unique(x), x)
## [1]  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29
2)此备选方案给出一个命名向量作为结果:

cumsum(table(x))
##  0  2  3 13 14 24 34 35 37 38 40 42 43 44 54 64 
##  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29 
1)如果
x
是输入xts对象,则会给出每个元素最后一次出现的索引

findInterval(unique(x), x)
## [1]  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29
2)此备选方案给出一个命名向量作为结果:

cumsum(table(x))
##  0  2  3 13 14 24 34 35 37 38 40 42 43 44 54 64 
##  4  6  8  9 13 14 15 16 17 19 20 22 24 25 28 29 

可以用:
跳过
rev()
s(!duplicated(df$testing,fromLast=TRUE))
@sindri_baldur我知道,但是要键入的字符更多!那么我们现在在打代码高尔夫<代码>哪个(!重复(df,f=T))
:)@sindri_baldur现在你在说话!我确实比cumsum(rle
可以跳过
rev()
s,使用:
哪个(!duplicated(df$testing,fromLast=TRUE))
@sindri_-baldur我知道,但是要输入的字符更多!所以我们现在正在打代码高尔夫?
哪个(!duplicated(df,f=T))
)@sindri_-baldur现在你在说话!我确实比cumsum(rle)更喜欢这个