Help, please, beginner)

The number Y is set. There is a data.frame of elements X and Z.

If Y is greater than X, then at these levels X must be summed Z between them.

How to write such a script?

    2 answers 2

    You can use the which function to find indices by condition

    I wrote an example, you can see how it works here.

     Y = 2; X = c(1, 3, 6); Z = c(1, 2, 3); df = data.frame(X, Z); which(df[,1] < Y) # Для того что бы понять как работает, выводим индексы найденные sum(df[which(df[,1] < Y), 2]) 

      If there are no restrictions on using third-party packages, then I would use dplyr . The code is more understood

       library (dplyr) Y <- 4 X <- c(1, 3, 5, 7) Z <- c(10, 20, 30, 40) df <- data.frame(X, Z) # фильтруем исходный `data frame`по условию df.t <- filter(df, df$X < Y) # в результате остается > df.t XZ 1 1 10 2 3 20 # просто суммируем по Z rslt <- sum(df.t$Z) > rslt [1] 30