I have a dataframe with the following data structure:
x <- c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "C", "C", "C", "C", "C", "C", "C", "C", "C")
y <- c("Y", "Y", "Y", "Y", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "N", "Y", "Y")
df <- data.frame(x, y)
I want to create a new column with unique values for each chunk of Y's in column 'y' and a value for each N in 'y' using dplyr, grouping by 'x'. For example:
z <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 3, 3)
df <- data.frame(x, y, z)
How would I do this?
I tried modifications to the answer to this question How to assign a unique ID number to each group of identical values in a column to no avail.