[R] Data Frame Manipulation: Time Series
Josip Dasovic
jjd9 at sfu.ca
Wed Jan 28 02:08:59 CET 2009
Hello Jim:
Yes, that's exactly what I needed!
Thank you!
Josip
----- Original Message -----
From: "jim holtman" <jholtman at gmail.com>
To: "Josip Dasovic" <j_dasovic at sfu.ca>
Cc: r-help at r-project.org
Sent: Tuesday, January 27, 2009 4:45:31 PM GMT -08:00 US/Canada Pacific
Subject: Re: [R] Data Frame Manipulation: Time Series
Is the what you are after:
> 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)))
> x <- split(df, df$country)
> do.call(rbind, lapply(x, function(.cty){
+ # create where the war starts
+ .start <- diff(c(0, .cty$war))
+ .cty[(.start == 1) & (.cty$war == 1),]
+ }))
country year war
Angola.1 Angola 1975 1
Angola.8 Angola 1982 1
Burundi.10 Burundi 1989 1
Burundi.14 Burundi 1993 1
Chad.17 Chad 1965 1
Chad.27 Chad 1975 1
On Tue, Jan 27, 2009 at 5:45 PM, Josip Dasovic <jjd9 at sfu.ca> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list