I have column in dataframe which has following structure in the rows.
first cycle
first cycle
shifting cycle
shifting cycle
shifting cycle
2nd cycle
2nd cycle
2nd cycle
shifting cycle
shifting cycle
3rd cycle
3rd cycle
I want to replace all rows with first entry of shifting cycle to shifting cycle 1 and 2nd entry of shifting cycle to shifting cycle 2. Basically it a string operation which I don' how to do it. Right I am doing it based on the value in other column but it is not appropriate to find the value in other column manually since value varies in many files.
My code
df$column <-str_replace(df$column, "Shifting cycle", "Shifting cycle 2")
df <- df %>% mutate(column = case_when(other_column ==30~ 'Shifting cycle 1' ,T~column))
so final output will be
first cycle
first cycle
shifting cycle 1
shifting cycle 1
shifting cycle 1
2nd cycle
2nd cycle
2nd cycle
shifting cycle 2
shifting cycle 2
3rd cycle
3rd cycle
df |> dplyr::mutate( x = ifelse( x == "shifting cycle", paste(x, cumsum(x == "shifting cycle" & dplyr::lag(x,default = "") != "shifting cycle")), x)). Thanks to R4DS community on slack. X is the column with existing data.