Here's a tidyverse solution:
library(tidyverse)
t <- c("a", "b", "c", "d", "e", "f")
# smaller so can see it
E <- tribble(
~x1, ~x2, ~x3, ~x4, ~x5, ~x6, ~x7, ~x8,
"a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "b", "b",
"a", "b", "c", "d", "e", "f", "a", "b"
) %>%
mutate(rowname = row_number()) %>%
add_row(tribble(
~x1, ~x2, ~x3, ~x4, ~x5, ~x6, ~x7, ~x8,
"a", "a", "a", "a", "a", "a", "a", "a"
)) # dummy row for lead to not NA
E
s_house_lookup <- enframe(s_house_gas <- 15:11, name = "rowname")
joined <- left_join(E, s_house_lookup)
(results <- joined %>%
mutate(across(
where(is.character),
~ if_else(. == "a" & (! lead(.) %in% c("a","d","e")), as.character(value), .)
)) %>%
slice(-nrow(E)) # remove dummy tow
)
x1 x2 x3 x4 x5 x6 x7 x8 rowname value
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <int> <int>
1 a a a a a a a a 1 15
2 a a a a a a a a 2 14
3 a a a a a a 13 13 3 13
4 a 12 12 a a 12 b b 4 12
5 a b c d e f a b 5 11
your example does have all a's on the 5th line as 11, is this because there is a 6th line, where they are different and not moving to d or e ? or some other principle ?