R: Count how many times value has occured before within certain range of rows

sakwa

I have a dataframe like this:

df <- data.frame("subj.no" = rep(1:3, each = 24), 
                 "trial.no" = rep(1:3, each = 8, length.out = 72), 
                 "item" = c(rep(c("ball", "book"), 4), rep(c("doll", "rope"), 4), rep(c("fish", "box"), 4), rep(c("paper", "candle"), 4), rep(c("horse", "marble"), 4), rep(c("doll", "rope"), 4), rep(c("tree", "dog"), 4), rep(c("ball", "book"), 4), rep(c("horse", "marble"), 4)),
                 "rep.no" = rep(1:4, each = 2, length.out = 72),
                 "DV" = c(1,0,1,0,1,0,0,1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,1,1,1,0,1,0,0,
                      1,0,0,1,1,0,1,0,0,1,1,1,1,0,0,0,0,0,0,1,0,1,0,1,1,0),)

I now want to create another column DV.no which says that the value 1 occurred the nth time within that combination of subj.no, trial.no and item. For DV==0, the value in the new column should be 0.

So the resulting vector should look like this:

DV.no = c(1,0,2,0,3,0,0,1,1,0,2,0,0,0,3,0,1,0,2,0,3,0,0,0,0,1,1,2,2,0,0,3,0,1,1,0,0,2,0,3,1,1,0,2,0,0,2,0,0,1,1,0,2,0,0,2,1,1,2,0,0,0,0,0,0,1,0,2,0,3,1,0)

So basically, for each unique combination of values in subj.no, trial.no and item, whenever the value of DV is 1, then 1 should be added to the count in the new variable.

(Remark: The column rep.no is not part of the relevant value combination. But it's in the df anyway, and since I didn't know if it's useful for the solution, I left it there.)

How can this be done in R?

akrun

We can do a group by cumsum on the 'DV' column

library(dplyr)
df %>%
    group_by(subj.no, trial.no, item) %>% 
     mutate(V.no = cumsum(DV)* DV)

Or in base R with ave

df$V.no <- with(df, DV *ave(DV, subj.no, trial.no, item, FUN = cumsum))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count how many times value has occured in rows

From Dev

How many times a date has occured in a Date range

From Dev

Count how many times a certain value per user has changed

From Dev

Excel - how to count how many times a word appears within a certain date range

From Dev

Count how many times a time occurs within a date range

From Dev

Count how many times a class has appeared before a specific occurance

From Dev

Excel: count occurences until value has occured n-times

From Dev

Count how many times a column contains a certain value in Pandas

From Mysql

Count how many times a rows enter time is within the enter and exit times of all other rows

From Dev

Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

From Dev

Count how many times a value in the database has changed?

From Dev

Count how many times certain pandas row has specific column value lower than another certain pandas row across many pandas dataframes

From Dev

Count how many rows have at least one certain value

From Dev

How can I count how many rows since a value has changed in a column vector in R?

From Dev

Count how many times values has changed in column using R

From PHP

Count how many times something was echoed in a range?

From Dev

Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

From Dev

Counting the times a string value has occured by group

From Dev

How to count how many times value1 appears with value2 within keys of an array?

From Dev

Count How Many Times Customer Has Purchases

From Dev

Conting how many times recurion occured in tree

From Dev

Count how many rows satisfy a certain filter

From Dev

Count how many times a value occurs in a range (based on the value in another column)

From Dev

Count how many rows have date within date range of each row for each ID Pandas

From Dev

How do I check how many times a term has occured in a list and its frequency?

From Dev

Python Pandas Iterate over rows and count how many times a unique pair of columns has shown

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

How to count how many times a meta_value appears in a column by certain meta_key?

From Dev

How to find out how many times a certain pattern in x rows corresponds to a value in another row?

Related Related

  1. 1

    Count how many times value has occured in rows

  2. 2

    How many times a date has occured in a Date range

  3. 3

    Count how many times a certain value per user has changed

  4. 4

    Excel - how to count how many times a word appears within a certain date range

  5. 5

    Count how many times a time occurs within a date range

  6. 6

    Count how many times a class has appeared before a specific occurance

  7. 7

    Excel: count occurences until value has occured n-times

  8. 8

    Count how many times a column contains a certain value in Pandas

  9. 9

    Count how many times a rows enter time is within the enter and exit times of all other rows

  10. 10

    Play Framework: Count how many times a key exists in a JSON tree and how many times is set to a certain value

  11. 11

    Count how many times a value in the database has changed?

  12. 12

    Count how many times certain pandas row has specific column value lower than another certain pandas row across many pandas dataframes

  13. 13

    Count how many rows have at least one certain value

  14. 14

    How can I count how many rows since a value has changed in a column vector in R?

  15. 15

    Count how many times values has changed in column using R

  16. 16

    Count how many times something was echoed in a range?

  17. 17

    Pandas: Is there a way to count how many times a certain valued row occurs after another set of valued rows

  18. 18

    Counting the times a string value has occured by group

  19. 19

    How to count how many times value1 appears with value2 within keys of an array?

  20. 20

    Count How Many Times Customer Has Purchases

  21. 21

    Conting how many times recurion occured in tree

  22. 22

    Count how many rows satisfy a certain filter

  23. 23

    Count how many times a value occurs in a range (based on the value in another column)

  24. 24

    Count how many rows have date within date range of each row for each ID Pandas

  25. 25

    How do I check how many times a term has occured in a list and its frequency?

  26. 26

    Python Pandas Iterate over rows and count how many times a unique pair of columns has shown

  27. 27

    Count how many times certain text combinations occurs in certain columns

  28. 28

    How to count how many times a meta_value appears in a column by certain meta_key?

  29. 29

    How to find out how many times a certain pattern in x rows corresponds to a value in another row?

HotTag

Archive