我在R中有一个很大的数据库,包含了不同股票的所有收盘价,我想得到每一只股票的最大值,与前几行股票相比,有点像这样:
Data max
1 1
2 2
1 2
3 3
5 5
6 6
3 6
2 6
1 6
4 6
5 6
7 7
3 7我试过使用rollmax,但是,由于它需要一个宽度,所以在某个时候它就停止工作了。提前谢谢。
发布于 2022-10-13 18:18:50
我们可以用cummax
df1$max <- cummax(df1$Data)-output
> df1
Data max
1 1 1
2 2 2
3 1 2
4 3 3
5 5 5
6 6 6
7 3 6
8 2 6
9 1 6
10 4 6
11 5 6
12 7 7
13 3 7数据
df1 <- structure(list(Data = c(1L, 2L, 1L, 3L, 5L, 6L, 3L, 2L, 1L, 4L,
5L, 7L, 3L)), row.names = c(NA, -13L), class = "data.frame")https://stackoverflow.com/questions/74060147
复制相似问题