Search code examples
rdataframedata-analysis

Check whether a vector element of one value is placed between vector elements of two other values in R


I did not find any method of checking whether categorical value elements of a vector are between other categorical value elements. A dataframe is given:

id    letter
1     B
2     A
3     B
4     B
5     C
6     B
7     A
8     B
9     C

Everything I found is related to numerical values and to the notion of general order (rather than to index of an element in a specific vector).

I want to add a new column with boolean values (1 if B is between A and C; 0 if B is between C and A) to the dataframe,

id    letter    between
1     B         0
2     A         NA
3     B         1
4     B         1
5     C         NA
6     B         0
7     A         NA
8     B         1
9     C         NA

Solution

  • Here's one solution, which I hope is fairly easy conceptually. For 'special' cases such as B being at the top or bottom of the list, or having an A or a C on both sides, I've set such values to 0.

    # Create dummy data - you use your own
    df <- data.frame(id=1:100, letter=sample(c("A", "B", "C"), 100, replace=T))
    
    # Copy down info on whether A or C is above each B
    acup <- df$letter
    for(i in 2:nrow(df))
      if(df$letter[i] == "B")
        acup[i] <- acup[i-1]
    
    # Copy up info on whether A or C is below each B
    acdown <- df$letter
    for(i in nrow(df):2 -1)
      if(df$letter[i] == "B")
        acdown[i] <- acdown[i+1]
    
    # Set appropriate values for column 'between'
    df$between <- NA
    df$between[acup == "A" & acdown == "C"] <- 1
    df$between[df$letter == "B" & is.na(df$between)] <- 0   # Includes special cases