Dear all,
Given the data frame below, I wish to accomplish the following:
Every time a new sequence starting with “d1” is found in the column “letter”, then the corresponding rows in the “target” column are filled with an item from the column “word”.
The instertion of items from “word” into "target" should follow a sequential order.
Here is a data sample:
library(tidyverse)
df <- tibble::tribble(
~letter, ~target, ~word,
"d1", NA, "pat",
"d2", NA, "tap",
"1", NA, "cap",
"2", NA, "house",
"3", NA, "patty",
"d1", NA, "cap",
"d2", NA, "tap",
"1", NA, "patty",
"2", NA, "house",
"3", NA, "pat",
"d1", NA, "pat",
"d2", NA, "tap",
"1", NA, "house",
"2", NA, "house",
"3", NA, "cap",
"d1", NA, "patty",
"d2", NA, "patty",
"1", NA, "pat",
"2", NA, "tap",
"3", NA, "pat",
"d1", NA, "house",
"d2", NA, "patty",
"1", NA, "cap",
"2", NA, "tap",
"3", NA, "cap"
)
head(df)
#> # A tibble: 6 x 3
#> letter target word
#> <chr> <lgl> <chr>
#> 1 d1 NA pat
#> 2 d2 NA tap
#> 3 1 NA cap
#> 4 2 NA house
#> 5 3 NA patty
#> 6 d1 NA cap
Created on 2023-06-02 with reprex v2.0.2
The following is an ideal(ized) rendering of the final result.
I would very much appreciate learning from you about the best way to approach this issue.
Thank you!
Dan