[R] Data Frame Manipulation: Time Series

Josip Dasovic
Tue, 27 Jan 2009 22:45:35 -0000

Dear R Helpers:

I have a data set where the unit of observation is country-year. I would like to
generate a new data set based on some inclusionary (exclusionary) criteria. Here
is an example of the type of data that I have.

df<-data.frame(cbind("country"=c(rep("Angola", 9),
rep("Burundi", 7), rep("Chad", 13)),
"year"=c(1975:1983, 1989:1995, 1965:1977)),
"war"=c(rep(1,2), rep(0,5), rep(1,2), rep(1,2), rep(0,2), rep(1,3),
rep(1,4), rep(0,6), rep(1,3)))
> df
   country year war
1   Angola 1975   1
2   Angola 1976   1
3   Angola 1977   0
4   Angola 1978   0
5   Angola 1979   0
6   Angola 1980   0
7   Angola 1981   0
8   Angola 1982   1
9   Angola 1983   1
10 Burundi 1989   1
11 Burundi 1990   1
12 Burundi 1991   0
13 Burundi 1992   0
14 Burundi 1993   1
15 Burundi 1994   1
16 Burundi 1995   1
17    Chad 1965   1
18    Chad 1966   1
19    Chad 1967   1
20    Chad 1968   1
21    Chad 1969   0
22    Chad 1970   0
23    Chad 1971   0
24    Chad 1972   0
25    Chad 1973   0
26    Chad 1974   0
27    Chad 1975   1
28    Chad 1976   1
29    Chad 1977   1

What I would like to do is to create a new data frame with only those
observations for which a) the "war" variable value is 1, (this ie easy
enough) and 2) it is the first (in time) instance of war for that country for
that war "episode" (each of the countries above has two war episodes).
Thus, the new data frame should look like this:

   country year war
1   Angola 1975   1
8   Angola 1982   1
10 Burundi 1989   1
14 Burundi 1993   1
17    Chad 1965   1
27    Chad 1975   1

Any suggestions as to how this can be done?

Thanks in advance,
Josip

R version 2.7.2 Patched (2008-09-20 r47259)
Mac OSX 10.5.5