Here is my dataframe:
structure(list(a = c(1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1)), .Names = "a", row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
Now I want to add an identification column that will act like index:
I mean that I want to add a column that will start from id = 1 and each time there is -1 to set it to be id = 2 and so on: Expected:
structure(list(a = c(1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1), b = c(1,
1, 2, 2, 2, 2, 3, 3, 3, 3, 3)), .Names = c("a", "b"), row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
Using the solution from R add index column to data frame based on row values didn't work for my needs.