SteveS
SteveS

Reputation: 4040

Adding index depend on variable in other column in a dataframe in R?

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

Answers (2)

acylam
acylam

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

kabr
kabr

Reputation: 1339

You can do it like this:

  1. create a new helper column, which has the value 1 in the first row and every time there is a -1.

  2. 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

Related Questions