Reputation: 4040
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.
Upvotes: 1
Views: 394
Reputation: 18661
You can also do it like this. Just cumsum
the logical vector created by a==-1
and add one to the result of that:
library(dplyr)
df1 %>%
mutate(b = cumsum(a == -1) + 1)
or with Base R:
df1$b = cumsum(df1$a == -1) + 1
Result:
# A tibble: 11 x 2
a b
<dbl> <dbl>
1 1 1
2 1 1
3 -1 2
4 1 2
5 1 2
6 1 2
7 -1 3
8 1 3
9 1 3
10 1 3
11 1 3
Data:
df1 = 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"))
Upvotes: 3
Reputation: 1339
You can do it like this:
create a new helper column, which has the value 1
in the first row and every time there is a -1
.
create the index
column by using the cumsum
function and delete the helper column
library(dplyr)
df %>%
mutate(helper = ifelse(row_number()==1, 1,
ifelse(a == -1, 1, 0))) %>%
mutate(index = cumsum(helper)) %>%
select(-helper)
Upvotes: 1