r - How to create a count variable by group for specific values in the variable of interest? -


Currently, I have to deal with the paradata (long format) generated by the software during a group's data collection phase.

How can I create a group containing a set of variable values ​​(such as id: gn_n if var1 == 2 in stata) in a variable )?

Basically the data looks like this:

  ID: VAR1: 1 2 1 1 1 2 2 2 2 3 2 2 3 2 3 2 3 2  

I

  `$ count.1 & lt; - Can create a variable count.1 using data - Away (Data $ VAR1, Data $ ID, FUN = seq_along) ID: VAR1: Count 1: 1 2 1 1 1 2 1 2 3 2 2 1 2 3 2 2 2 3 3 2 1 3 2 2 3 2 3 How can I make a variable count count 2. Number of events in VAR1 of ID 2?  
  ID: VAR1: Count.1: count.2: 1 2 1 1 1 1 2 NA 1 2 3 2 2 2 1 1 2 3 2 NA 2 2 3 2 3 1 1 NA 3 2 2 1 3 2 3 2  

Data:

  id = c (1,1,1,2,2,2,3,3 , 3) VAR1 = c (2,2,2, 2,3,2,1,2,2) data & lt; - as.data.frame (cbind (id, var1))  

thanks in advance !!!

Try

  data $ count.2. & Lt; - Data with Away (VAr1 == 2, id, Fun = function (X) Eiffel (X, Cosmam (x), NA)) Data $ count 2. [1] 1 NA 2 1 NA 2 NA 1 2  

or data.table

  Using Library (Data Eligible) SetDi [Data] [VAR1 == 2, count .2: = 1:., By = id] [] # id VAR1 count.2 # 1: 1 2 1 # 2: 1 1 NA # 3: 1 2 2 # 4: 2 2 # 5: 2 3 NA # 6: 2 2 2 # 7: 3 1 NA # 8: 3 2 1 # 9: 3 2 2  

or dplyr Using

  Library (dplyr) Data%>% group_by (id)%>% mutate (count.2 = ifelse (VAR1 == 2, cumsum (VAR1 == 2) ), NA))  

Comments