counting how many times a variable changes score by group

Alex

I have a very basic question that I am a little struggling with, I have a panel large panel dataset that looks something like this:

df <- data.frame(id= c(1,1,1,2,2,2,3,3,3,4,4,4), time=c(1,2,3,1,2,3,1,2,3,1,2,3), x = c(0,0,0,0,1,1,0,0,1,0,1,2))

I would like to find a compact way to count how many times my x variable changes for every id. The final dataset should look something like this

df <- data.frame(id= c(1,1,1,2,2,2,3,3,3,4,4,4), time=c(1,2,3,1,2,3,1,2,3,1,2,3), x = c(0,0,0,0,1,1,0,0,1,0,1,2),count= c(0,0,0,1,1,1,1,1,1,2,2,2))

Ideally I would like to use dplyr

I was thinking i should do something like like

library(dplyr)
df <- df %>% group_by(id) %>% mutate(count=)

But I am not sure how to complete it because I don't know what kind of command I can use to count changes of scores.

Thanks a lot in advance for your help

27 ϕ 9

You can use the sum of the lagged difference of x not equal to zero:

library(dplyr)

 df %>% 
   group_by(id) %>%
   mutate(count = sum(diff(x) != 0))

   id time x count
1   1    1 0     0
2   1    2 0     0
3   1    3 0     0
4   2    1 0     1
5   2    2 1     1
6   2    3 1     1
7   3    1 0     1
8   3    2 0     1
9   3    3 1     1
10  4    1 0     2
11  4    2 1     2
12  4    3 2     2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting how many times variable has changed

From Dev

Counting how many times a boolean value changes in SQL Server

From Dev

Pandas counting how many times value changes for a specific index

From Dev

Pandas counting how many times a value has been counted in a Group by

From Dev

Counting how many times a loop runs

From Dev

Counting how many times each vowel appears

From Dev

Counting how many times an image appears on screen

From Dev

SAS - Increment a counting variable increasing whenver other variable in a group changes

From Dev

Count how many times a character changes in a string

From Dev

Track how many times a cell changes

From Dev

Counting how many times the biggest number in a column appear in Excel

From Dev

Counting how many times each unique element appeared in `select` query

From Dev

Counting how many times each unique element appeared in the table

From Dev

Counting how many times there are blank lists in a list of list

From Dev

Counting how many times a character is typed without using string

From Dev

Counting how many times a specific checked value occurs in AngularJS

From Dev

Counting how many times a base function is being used, Python

From Java

Counting how many times my Android app has been opened

From Dev

Python: counting how many times a given line is executed

From Dev

Counting how many times a row occurs in a matrix (numpy)

From Dev

Counting how many times a function is called recursively (Python)

From Dev

counting how many times a name appears in a specific month

From Dev

counting how many times an item appears in a multidimensional array in javascript

From Dev

Counting how many times the click() function has been used

From Dev

Regular expressions - counting how many times a word appears in a text

From Dev

Counting how many times a string shows in a column in R

From Dev

decorator with parameter and counting how many times func called

From Dev

Counting how many times a number appears in a random array

From Dev

Searching a txt file for a string and then counting how many times it appears

Related Related

  1. 1

    Counting how many times variable has changed

  2. 2

    Counting how many times a boolean value changes in SQL Server

  3. 3

    Pandas counting how many times value changes for a specific index

  4. 4

    Pandas counting how many times a value has been counted in a Group by

  5. 5

    Counting how many times a loop runs

  6. 6

    Counting how many times each vowel appears

  7. 7

    Counting how many times an image appears on screen

  8. 8

    SAS - Increment a counting variable increasing whenver other variable in a group changes

  9. 9

    Count how many times a character changes in a string

  10. 10

    Track how many times a cell changes

  11. 11

    Counting how many times the biggest number in a column appear in Excel

  12. 12

    Counting how many times each unique element appeared in `select` query

  13. 13

    Counting how many times each unique element appeared in the table

  14. 14

    Counting how many times there are blank lists in a list of list

  15. 15

    Counting how many times a character is typed without using string

  16. 16

    Counting how many times a specific checked value occurs in AngularJS

  17. 17

    Counting how many times a base function is being used, Python

  18. 18

    Counting how many times my Android app has been opened

  19. 19

    Python: counting how many times a given line is executed

  20. 20

    Counting how many times a row occurs in a matrix (numpy)

  21. 21

    Counting how many times a function is called recursively (Python)

  22. 22

    counting how many times a name appears in a specific month

  23. 23

    counting how many times an item appears in a multidimensional array in javascript

  24. 24

    Counting how many times the click() function has been used

  25. 25

    Regular expressions - counting how many times a word appears in a text

  26. 26

    Counting how many times a string shows in a column in R

  27. 27

    decorator with parameter and counting how many times func called

  28. 28

    Counting how many times a number appears in a random array

  29. 29

    Searching a txt file for a string and then counting how many times it appears

HotTag

Archive