[R] How to create these variables in R?
Faradj Koliev
faradj.g at gmail.com
Tue Jun 28 13:34:55 CEST 2016
Dear all,
Let’s say that I have a dataset that looks something like this:
Subject Year A
A 1990 1
A 1991 1
A 1992 1
A 1993 1
A 1994 0
A 1995 0
B 1990 1
B 1991 0
B 1992 1
B 1993 1
C 1991 1
C 1992 1
C 1993 0
C 1994 1
D 1991 0
D 1992 1
D 1993 1
D 1994 0
D 1995 0
D 1996 1
D 1997 1
What I would like to do is to create the following three new variables: A1, A2, and A3
The variable A1 should capture/count all 1’s in the variable A that are in a row (counting), for each subject-year – but it should restart counting if there
are two 0’s in a row (displayed
below).
The variable A2 should capture 1-2’s (range) in the A1. Displayed in the example below.(subject-year)
The variable A3 should capture all values in the variable A1 that are more than 2, also displayed in the example data below.(subject-year)
See the example below for illustration of these variables
Subject Year A A1 A2 A3
A 1990 1 1 1 0
A 1991 1 2 1 0
A 1992 1 3 0 1
A 1993 1 4 0 1
A 1994 0 0 0 0
A 1995 0 0 0 0
B 1990 1 1 1 0
B 1991 0 1 1 0
B 1992 1 2 1 0
B 1993 1 3 0 1
C 1991 1 1 1 0
C 1992 1 2 1 0
C 1993 0 2 1 0
C 1994 1 3 0 1
D 1991 0 0 0 0
D 1992 1 1 1 0
D 1993 1 2 1 0
D 1994 0 0 0 0
D 1995 0 0 0 0
D 1996 1 1 1 0
D 1997 1 2 1 0
I’ve no clue where to start at this stage –I’d appreciate any suggestions and help.
[[alternative HTML version deleted]]
More information about the R-help
mailing list