[R] diff() for panel data

jim holtman jholtman at gmail.com
Sun Sep 21 04:40:09 CEST 2008


use "split' to split your dataframe by country and then do the diff.

On Sat, Sep 20, 2008 at 8:54 PM, Gabriel Paul Mihalache
<mihalache at gmail.com> wrote:
> I was suggested that more details with help re: my question on first
> differences in panel data...
> The data set in question is PWT6.2:
>
>> str(pwt6.2)
> 'data.frame':   10340 obs. of  27 variables:
>  $ country: Factor w/ 188 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
>  $ isocode: Factor w/ 188 levels "AFG","ALB","DZA",..: 1 1 1 1 1 1 1 1 1 1 ...
>  $ year   : int  1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 ...
>  $ pop    : num  8150 8284 8425 8573 8728 ...
>  ...
>  $ cgdp   : num  NA NA NA NA NA NA NA NA NA NA ...
>  ...
>  $ grgdpch: num  NA NA NA NA NA NA NA NA NA NA ...
>
> The panel has countries as units and years for time.
> What I want to do is have a fist different of logs of cgdp and pop.
>
> The reason why I can't use diff(log(pop)) is because when the data for
> a country ends, e.g. Afghanistan 2004, the next observation belongs to
> another country, e.g. Albania 1950, and R will do a first log
> difference between the population of Albania in 1950 and that of
> Afghanistan in 2004, instead of NA (since I don't have data for
> Albania 1949).
>
> I need a diff() that's aware of the panel structure of the data (i.e.
> the countries).
> The plm package does this, but only in a regression. I found no
> function that I can use to save the log differences.
>
> Thank you for your time,
> Gabriel
>
> ______________________________________________
> 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