On 28/06/2016 7:34 AM, Faradj Koliev wrote:
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)

A1 has a pretty complicated definition. There's likely a vectorized way to write it, but it won't be easy to read. I'd just use a loop. Your rules are a little ambiguous (are 0 and 1 the only possible values?) so I won't try, but it should be straightforward for you to write the loop.

A2 and A3 are easy:

A2 <- as.numeric(A1 %in% 1:2)
A3 <- as.numeric(A1 > 2)

Duncan Murdoch



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]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to