获得连续高值data.frame列的有效方法

时间:2021-09-06 20:12:08

Lets say I have the following data.frame in R

假设我在R中有以下data.frame

df <- data.frame(order=(1:10),value=c(1,7,3,5,9,2,9,10,2,3))

Other than looping through data an testing whether value exceeds previous high value how can I get successive high values so that I can end up with a table like this

除了循环数据之外,测试值是否超过先前的高值我如何获得连续的高值,以便我最终得到这样的表

order   value
 1        1
 2        7
 5        9
 8       10

TIA

TIA

2 个解决方案

#1


1  

Here's one option, if I understood the question correct:

如果我理解问题是正确的,这是一个选项:

df[df$value > cummax(c(-Inf, head(df$value, -1))),]
#  order value
#1     1     1
#2     2     7
#5     5     9
#8     8    10

I use cummax to keep track of the maximum of column "value" and compare it (the previous row's cummax) to each "value" entry. To make sure the first entry is also selected, I start by "-Inf".

我使用cummax来跟踪列“值”的最大值,并将它(前一行的cummax)与每个“值”条目进行比较。为了确保第一个条目也被选中,我以“-Inf”开头。

#2


0  

"get successive high values (of value?)" is unclear. It seems you want to filter only rows whose value is higher than previous max.

“获得连续的高值(价值?)”尚不清楚。您似乎只想过滤其值高于之前最大值的行。

First, we reorder your df in increasing order of value... (not clear but I think that's what you wanted)

首先,我们按照递增的价值重新排序您的df ...(不清楚,但我认为这是您想要的)

Then we use logical indexing with diff()>0 to only include strictly-increasing rows:

然后我们使用diff()> 0的逻辑索引来仅包含严格增加的行:

rdf <- df[order(df$value),]

rdf[ diff(rdf$value)>0, ]
   order value
1      1     1
9      9     2
10    10     3
4      4     5
2      2     7
7      7     9
8      8    10

#1


1  

Here's one option, if I understood the question correct:

如果我理解问题是正确的,这是一个选项:

df[df$value > cummax(c(-Inf, head(df$value, -1))),]
#  order value
#1     1     1
#2     2     7
#5     5     9
#8     8    10

I use cummax to keep track of the maximum of column "value" and compare it (the previous row's cummax) to each "value" entry. To make sure the first entry is also selected, I start by "-Inf".

我使用cummax来跟踪列“值”的最大值,并将它(前一行的cummax)与每个“值”条目进行比较。为了确保第一个条目也被选中,我以“-Inf”开头。

#2


0  

"get successive high values (of value?)" is unclear. It seems you want to filter only rows whose value is higher than previous max.

“获得连续的高值(价值?)”尚不清楚。您似乎只想过滤其值高于之前最大值的行。

First, we reorder your df in increasing order of value... (not clear but I think that's what you wanted)

首先,我们按照递增的价值重新排序您的df ...(不清楚,但我认为这是您想要的)

Then we use logical indexing with diff()>0 to only include strictly-increasing rows:

然后我们使用diff()> 0的逻辑索引来仅包含严格增加的行:

rdf <- df[order(df$value),]

rdf[ diff(rdf$value)>0, ]
   order value
1      1     1
9      9     2
10    10     3
4      4     5
2      2     7
7      7     9
8      8    10