I'm experiencing some problems describing what I want to create. So let's say that I have a dataset like the one below:
country year    X
A       1990    0
A       1991    1
A       1992    2
A       1993    3
A       1994    3
B       1990    1
B       1991    2
B       1992    3
B       1993    3
C       1990    0
C       1991    1
C       1992    2
C       1993    3
C       1994    4
The variable X counts the number of times a country appears in the media. Note though that it sometimes stays on the same number for several years – this is because no new appearances are reported for that year.
So I want to create a variable that only captures increases. Let's call this variable "Xnew". I give an example of what it would look like below:
country year    X   Xnew
A       1990    0   0
A       1991    1   1
A       1992    2   1
A       1993    3   1
A       1994    3   0
B       1990    1   1
B       1991    2   1
B       1992    3   1
B       1993    3   0
C       1990    0   0
C       1991    1   1
C       1992    2   1
C       1993    3   1
C       1994    4   1
As you see, the "Xnew" variable is a binary one, where 1 captures only increases, and 0 otherwise.
My attempt at creating this variable was the following:
> data$Xnew <- as.numeric(X >1)
But it doesn't really do what I want, though I sense that the solution lies somewhere close to this. Any suggestions? Thanks!
A reproducible sample:
> dput(data)
structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    year = c(1990L, 1991L, 1992L, 1993L, 1994L, 1990L, 1991L, 
    1992L, 1993L, 1990L, 1991L, 1992L, 1993L, 1994L), X = c(0L, 
    1L, 2L, 3L, 3L, 1L, 2L, 3L, 3L, 0L, 1L, 2L, 3L, 4L)), .Names = c("country", 
"year", "X"), class = "data.frame", row.names = c(NA, -14L))
 
     
     
     
    