Search highest values of intervals

Hi to everyone,

I'm searching for a R script to find the 2 highest values for several interval of a data frame.
For example:

I want to find the 2 highest values of my data frame for each 0.2 m/s velocity from 3 to 10.
How can I write it on R script?

My aim is to have a linear regression of all these highest values after maybe using LOESS fonction.

Anybody has any tips on it?

Best

library(tidyverse)
(df <- tibble(
  velocity = round(runif(n = 1000, min = 0, max = 10), 1),
  acceleration = round(rnorm(n = 1000, 4, 2), 2)
))


(filtered_df <- mutate(df %>% filter(between(velocity, 3, 10)),
  cutvel = ggplot2::cut_interval(
    x = velocity,
    length = .2
  )
))

(filtered_split_groups <- group_split(group_by(filtered_df, cutvel)))

(top2_of_each_as_list <- purrr::map(
  filtered_split_groups,
  ~ top_n(., 2, wt = acceleration)
))

(top2_as_one_df <- bind_rows(top2_of_each_as_list))

Wahou thank you for your answer @nirgrahamuk!! very impressive and helpful for me!!

I would like to highlights it on the graph and make a LOESS regression to have this type of graph

What do you think about it?

:pray:

I recommend you use ggplot2 package from the tidyverse to plot it.

I link to a highly recommended tutorial if its your first time plotting with it.

:handshake:

Thank you very muche for your answear!! I'm going to dig deeper on it :nerd_face:

Hey Houston, I have still a problem!!

Why when I use ggplot_smooth, my lm is correct see

(filtered_split_groups <- group_split(group_by(filtered_df, cutvel)))

(top2_of_each_as_list <- purrr::map(
filtered_split_groups,
~ top_n(., 2, wt = Acceleration)
))

(top2_as_one_df <- bind_rows(top2_of_each_as_list))

p = ggplot() +
geom_point(data = data_final, aes(x = data_final$Velocity, y = data_final$Acceleration)) +
geom_point(data = top2_as_one_df, aes(x = top2_as_one_df$Velocity, y = top2_as_one_df$Acceleration), color = "red") +
geom_smooth(mapping = aes (x = top2_as_one_df$Velocity, y = top2_as_one_df$Acceleration), data = top2_as_one_df, color = "Red", method = "lm", fill="Red",formula = y ~ x) +

plot(p)

And when I'm trying to use directly the lm function, it is not correct...

plot(data_final,xlim=c(0,10),ylim=c(0,10),
main="AS profile practice",
xlab="Velocity (m/s)",
ylab="Acceleration (m/s²)")
abline(lm1, col="red",lxd=2)

@nirgrahamuk Have you got a thought on it?

Thanks

The first thing I must call to your attention, is that in ggplot2 , as you assign data= params, then you should not mention the dataframes again when you assign variables to aes aethetics, the point of setting the data param was to let ggplot2 know which dataframe to take variables from so remove the dataframename$ part in the variable assignments.

The second question you ask is about plotting a lm1 object, but you haven't shared the code for how you made it, so I can't see if you may have made any mistake with it or not.

I made a mistake in my lm formula you're right.
I did lm(x~y) and not lm(y~x)

Thanks a lot for helping me in this project @nirgrahamuk

Best

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.