Reputation: 61
I have a data look like:
power = seq(1:10)
Rsq = c(-0.503268561,-0.337920056,-0.000470763,0.204181075,0.299591119,0.376839371,0.424761718, 0.424487633, 0.642269314, 0.194640766)
df = data.frame(power,Rsq)
Now, I want to choose max value FOR THE FIRST TIME in the column 'Rsq'. From that, my expected result look like:
power ------- Rsq
7 ----------0.424761718
Upvotes: 0
Views: 59
Reputation: 887118
We can get the diff
erence of adjacent elements in 'Rsq', check if it is less than 0 and get the index of the max
library(dplyr)
df %>%
slice(which.max(c(diff(Rsq), NA) < 0))
# power Rsq
#1 7 0.4247617
Upvotes: 1