I have this example dataframe in R.
id <- c(1001, 1001, 1002)
status <- c("dog", "cat", "mouse")
col3 <- c(5, 8, 4)
col4 <- c(9, 9, 6)
df <- data.frame(id, status, col3, col4)
The first column is ID, but 1001 is duplicated. The second column is unique status. The id 1001 has "dog" and "cat" in a separate row. col 3 and col4 contain information for the data frame transformation. col4 designates the "maximum numbers of rows" for each IDs. col3 indicates the starting row number that the status will be assigned. Based on these conditions, I would like to have the following dataframe as a product.
id <- c(1001, 1001, 1001, 1001, 1001, 1001,1001, 1001,1001, 1002, 1002, 1002, 1002, 1002, 1002)
status <- c("Not assigned", "Not assigned","Not assigned","Not assigned", "dog", "dog", "dog", "cat", "cat", "Not assigned", "Not assigned","Not assigned", "mouse", "mouse", "mouse")
df_results <- data.frame(id, status)
Please note that the rows before the designated starting rows are assigned as "Not assigned". My idea was to create a new column with mutation(lead()) and use "uncount()" to repeat the rows as needed, but difficult to assign the items in "status" column accordingly. Any help will be appreciated.