please, how filter in intervals to obtein maximum in that intervals (using loop for)

Hi, I am trying to filter in a secuence by different intervals to obtein the maximun value in each interval...

I am trying to do it filtering using a loop... but I am not able to do it

Please, could anyone help me?

This is an example of dataframes to do it:

df <- data.frame(tibble(
  PK = 1:100,
  z = PK^2  #where PK is the secuence where I want to filter ache interval
))

df1 <- data.frame(tibble(
  INI = seq(from = 10, to = 90, by = 10), #every start of each interval
  FIN = seq(from = 20, to = 100, by = 10) #every end of each interval
))

Could someone help me?

Thanks in advance

Javi (from Spain)

Hi,

I hope I understood what you wanted to achieve. Please find my code below:

df <- data.frame(tibble(
  PK = 1:100,
  z = PK^2  #where PK is the secuence where I want to filter ache interval
))

df1 <- data.frame(tibble(
  INI = seq(from = 10, to = 90, by = 10), #every start of each interval
  FIN = seq(from = 20, to = 100, by = 10) #every end of each interval
))


#Max value per interval 



for (i in 1:length(df1[,1])) {
  max_in_interval[i] <- max(df[df1$INI[i]:df1$FIN[i],2])
}

max_in_interval

[1]   400   900  1600  2500  3600  4900  6400  8100 10000

Thanks Patryk! I think you understood it perfectly reading your solution and result, but when I copy and paste it on my Rstudio it gives me an error:

(attached image)

Would I need to load any library or install any package?

Thanks in advance! Javier

Hi Javier,

Add this before the for loop:

max_in_interval <- c()

The variable max_in_interval didn't exist yet.

I would advise you to skim through this book Hands-On Programming with R . It will teach you all the basics about R programming and you'll be able to solve problems like this on your own.

Best regards,

Thanks! it works :slight_smile:

I am trying to use it in my real case and the problem is that Nº rows are not the same values than df$PK... and in the example the number of rows is the same value than df$PK

If the example was this another one:

df <- data.frame(tibble(
PK = 5:105,
z = PK^2 #where PK is the secuence where I want to filter ache interval
))

df1 <- data.frame(tibble(
INI = seq(from = 10, to = 90, by = 10), #every start of each interval
FIN = seq(from = 20, to = 100, by = 10) #every end of each interval
))

Could you make the solution?

sorry by the confusion and thanks again

I got it! Thanks Patryk because I used part of your code to get it.

This is the final code (I am sure that it migth be improbable, but it works :slight_smile: ):

df <- data.frame(tibble(
PK = 5:120,
z = PK^2 #where PK is the secuence where I want to filter ache interval
))

df1 <- data.frame(tibble(
INI = seq(from = 10, to = 90, by = 10), #every start of each interval
FIN = seq(from = 20, to = 100, by = 10) #every end of each interval
))

df <- df %>%
mutate(id = seq(1, nrow(df), by=1))

df_INI <- c()

for (i in 1:length(df1[,1])) {
 df_INI[i] <- min(filter(df, PK>=df1$INI[i])[i,3]) }

 df_INI <- data.frame("INI"=df_INI)
 
 c = data.frame(id = seq(0, (nrow(df_INI)-1), by=1))
 
 df_INI$INI = df_INI$INI - c$id
  
df_FIN <- c()

for (i in 1:length(df1[,1])) {
 df_FIN[i] <- min(filter(df, PK>=df1$FIN[i])[i,3]) }

 df_FIN <- data.frame("FIN"=df_FIN)
 
 c = data.frame(id = seq(0, (nrow(df_FIN)-1), by=1))
 
 df_FIN$FIN = df_FIN$FIN - c$id

m = c(df_INI$INI)
n = c(df_FIN$FIN)

df10 <- data.frame(INI=m, FIN=n)

 

max_in_interval <- c()

for (i in 1:length(df1[,1])) {
  max_in_interval[i] <- max(df[df10$INI[i]:df10$FIN[i],2])}

data <- data.frame("INI" = df1$INI, "FIN" = df1$FIN, "Z" = max_in_interval)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.