I am cleaning up some data imported from Excel. I am trying to create a column of values based on the position of a row in a data frame. Specifically, I am trying to assign a value to rows between two rows with specific character values using mutate()
and ifelse()
. Here is a very simplified example of the data I am working with:
a b
[1,] "5" "yes"
[2,] "6" "no"
[3,] "7" "no"
[4,] "2" "yes"
[5,] "apple" NA
[6,] "4" "yes"
[7,] "1" "no"
[8,] "banana" NA
[9,] "6" "yes"
[10,] "3" "yes"
I want to create a c
column that returns a character value of colors, where the rows between "apple"
and "banana"
(row numbers [6] and [7])are assigned a c
column value of "red"
, and all other rows are assigned a value of "blue"
. Is there a way to do this? Please let me know if I can explain my problem more clearly!