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
))
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
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.
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
))
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 ):
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)