1

looking to write an R script that will search a column for a specific value and begin sub setting rows until a specific text value is reached.

Example:

    X1  X2 
[1,] "a" "1"
[2,] "b" "2"
[3,] "c" "3"
[4,] "d" "4"
[5,] "e" "5"
[6,] "f" "6"
[7,] "c" "7"
[8,] "k" "8"

What I'd like to do is search through X1 until the letter 'c' is found, and begin to subset rows until another letter 'c' is found, at which point the subset procedure would stop. Using the above example, the result should be a vector containing c(3,4,5,6,7).

Assume there will be no more than 2 rows where X1 equals 'c'

Any help is greatly appreciated.

sirandy
  • 1,834
  • 5
  • 27
  • 32
the_witch_dr
  • 123
  • 1
  • 9
  • 1
    What if there are three c's found? Also, please share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can copy/paste into R for testing. – MrFlick Dec 10 '18 at 18:11
  • Good point and something I forgot to mention - no character will ever be duplicated more than twice so there will be clear beginning and end points – the_witch_dr Dec 10 '18 at 18:13
  • `Reduce(seq, which(your_data[, 1] == 'c')[1:2])` – IceCreamToucan Dec 10 '18 at 18:21

1 Answers1

4

You can lookup where a value is with the function which, and use that as in index to get the values you are looking for. If you want everything from the first to the second "c", it would look like this:

indices <- which(df$X1=='c')
range <- indices[1]:indices[2]
df$X2[range]
Emil Bode
  • 1,784
  • 8
  • 16