samedi 18 avril 2020

Conditional Statements: selecting/assigning a variable per row

I have a data set with 2 VPs and 350 interval values for each. I am writing an if loop to select when a minimum value of VP1 overlaps with the maximum value of VP2.

The data usually sorts by VP, but I arranged to sort by minimum since it is a timeframe.

I ran the following code that worked to assign 0 or 1 when the values overlap the previous item, but it does not account for what the previous item is (ie. whether the previous item is VP1 or VP2).

    for (i in 2:length(df$newvariable)) {
    if (df$minimum[i] > df$maximum[i-1]){
    df$newvariable[i] <- 0
    } else {
    df$newvariable[i] <- 1
    }
    }

I want to say “if df$minimum[i] of VP1 > df$maximum[i] of VP2, then df$newvariable = 0. Otherwise, df$newvariable = 1.”

I have not been able to find how to make it conditional per each row and loop again. Does anyone have any recommendations?

Many thanks.

Aucun commentaire:

Enregistrer un commentaire