Generateing a per-group sequence based on a specific value in an existing column

Orion

Related to my other question - given the following data frame

df0 <- data.frame (id  = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3),
                   val = c(0, 1, 1, 0, 0, 0, 1, 0, 1, 1))

what is the most straightforward way (i.e. one liner) to add a third column in which, for each id, sequence numbers appear only when val == 1, like the following?

   id val  seq
1   1   0    NA
2   1   1    1
3   1   1    2
4   2   0    NA
5   2   0    NA
6   2   0    NA
7   3   1    1
8   3   0    NA
9   3   1    2
10  3   1    3
Arun

Using data.table:

require(data.table)
setDT(df0)[val == 1L, seq := seq_len(.N), by=id]
#     id val seq
#  1:  1   0  NA
#  2:  1   1   1
#  3:  1   1   2
#  4:  2   0  NA
#  5:  2   0  NA
#  6:  2   0  NA
#  7:  3   1   1
#  8:  3   0  NA
#  9:  3   1   2
# 10:  3   1   3

.N contains the number of observations per group. Start with the HTML vignettes for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Subtract Column Values Based on Second Column Value with Group By Statement

From Dev

Reset sequence to a specific value

From Dev

Mysql query for sum based off a value in a column group by that column

From Dev

group by column not having specific value

From Dev

New column in Pandas dataframe based on value of variable in existing column

From Dev

Extract column name and specific value based on a condition

From Dev

Assign value to group based on condition in column

From Dev

Group by query based on other column value

From Dev

Select new column based on existing value

From Dev

How to group multiple values into one based on specific column value?

From Dev

Create new column in Pandas dataframe based on latest value per ID

From Dev

Group words based on the value of the first column

From Dev

Group items in a table based on value in column and count

From Dev

Mysql query for sum based off a value in a column group by that column

From Dev

How to add new column, where the value is based on existing columns with awk

From Dev

Group by column name -> specific value in that column

From Dev

Select rows based on value in specific column

From Dev

New column in Pandas dataframe based on value of variable in existing column

From Dev

print sum based on a value of specific column

From Dev

Extract column name and specific value based on a condition

From Dev

mysql group records based on a column value

From Dev

How can I group data based on range of values in specific column

From Dev

Excel - calculate average of values in one column based on another grouping column. The number of rows is not constant per group

From Dev

how to group column as per its unique value and get count in to different column as per its value?

From Dev

Mapping a value into a specific column based on annother column

From Dev

Count of cases per group, based on a min value of another variable, also per same groups

From Dev

Group by specific value and generate sequence number using xslt 1.0

From Dev

Sum group of rows based on value in a column

From Dev

Group rows based on column sum value

Related Related

  1. 1

    SQL Subtract Column Values Based on Second Column Value with Group By Statement

  2. 2

    Reset sequence to a specific value

  3. 3

    Mysql query for sum based off a value in a column group by that column

  4. 4

    group by column not having specific value

  5. 5

    New column in Pandas dataframe based on value of variable in existing column

  6. 6

    Extract column name and specific value based on a condition

  7. 7

    Assign value to group based on condition in column

  8. 8

    Group by query based on other column value

  9. 9

    Select new column based on existing value

  10. 10

    How to group multiple values into one based on specific column value?

  11. 11

    Create new column in Pandas dataframe based on latest value per ID

  12. 12

    Group words based on the value of the first column

  13. 13

    Group items in a table based on value in column and count

  14. 14

    Mysql query for sum based off a value in a column group by that column

  15. 15

    How to add new column, where the value is based on existing columns with awk

  16. 16

    Group by column name -> specific value in that column

  17. 17

    Select rows based on value in specific column

  18. 18

    New column in Pandas dataframe based on value of variable in existing column

  19. 19

    print sum based on a value of specific column

  20. 20

    Extract column name and specific value based on a condition

  21. 21

    mysql group records based on a column value

  22. 22

    How can I group data based on range of values in specific column

  23. 23

    Excel - calculate average of values in one column based on another grouping column. The number of rows is not constant per group

  24. 24

    how to group column as per its unique value and get count in to different column as per its value?

  25. 25

    Mapping a value into a specific column based on annother column

  26. 26

    Count of cases per group, based on a min value of another variable, also per same groups

  27. 27

    Group by specific value and generate sequence number using xslt 1.0

  28. 28

    Sum group of rows based on value in a column

  29. 29

    Group rows based on column sum value

HotTag

Archive