Re: [R] data frame

From: Sarah Goslee <sarah.goslee_at_gmail.com>
Date: Thu, 17 Jun 2010 06:32:23 -0400

You've posted this repeatedly, and yet received no answer. Perhaps that is because you haven't read the posting guide! You didn't provide a reproducible example, you didn't tell us where ddply came from, you didn't tell us what was wrong with the code you suggested. It would also be rather easier to help you if you put your statement of requirements in R syntax form. In your example the results column suddenly switched from R_pivot to series - which do you want?

In the meantime, what's wrong with simply:

x[x$YEAR == 2006, "R_pivot"] <- x[x$YEAR == 2007, "R_pivot"] -
x[x$YEAR == 2007, "Delta"]
x[x$YEAR == 2005, "R_pivot"] <- x[x$YEAR == 2006, "R_pivot"] -
x[x$YEAR == 2006, "Delta"]

It works on your sample dataframe, and would work on a larger frame sorted in the same way.

> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Sarah

On Thu, Jun 17, 2010 at 3:04 AM, n.vialma_at_libero.it <n.vialma_at_libero.it> wrote:
>
> Dear list,
> I have the following problem. I have a data frame like this
>
>
>
> CLUSTER        YEAR      variable        Delta         R_pivot
>
> M1                     2005         EC01           NA              NA
>
> M1                     2006         EC01            2                NA
>
> M1                     2007         EC01            4                5
>
> M2                     2005          EC01          NA              NA
>
> M2                     2006          EC01           5                NA
>
> M2                     2007          EC01           8                 7
>
> M1                     2005          EC02           NA               NA
>
> M1                     2006          EC02            3                 NA
>
> M1                    2007            EC02           1                 8
>
> M2                    2005            EC02           NA               NA
>
> M2                    2006            EC02           9                 NA
>
> M2                    2007            EC02            6                 10
>
>
>
>
> I'm trying to build the time series of the variables by applying the following formulas in a recursive way(by starting from the value of R_pivot at time 2007)
>
> R_EC01(2006)=R_EC01(2007)-Delta_EC01(2007)
>
> R_EC01(2005)=R_EC01(2006)-Delta_EC01(2006)
> In terms of number I would have:
> R_EC01(2006)=5-4=1
> R_eco1(2005)=1-2=-1
>
> And the same should be done for variable EC02. In addition, this calculations should be down grouping by variable e cluster..so the result should be
> CLUSTER        YEAR      variable        series
>
> M1                     2005         EC01             -1
>
>
> M1                     2006         EC01             1
>
>
> M1                     2007         EC01              5
>
>
> M2                     2005          EC01             -6
>
>
>
> M2                     2006          EC01             -1
>
> M2                     2007          EC01              7
>
>
>
> M1                     2005          EC02              4
>
> M1                     2006          EC02               7
>
>
> M1                    2007            EC02              8
>
>
> M2                    2005            EC02             -5
>
>
> M2                    2006            EC02            4
>
>
> M2                    2007            EC02             10
> I applied the following formula which gives me a partial good result but not at all:
> series=ddply(x,.(variable,CLUSTER),transform,series=rev(c(R_pivot,rev(R_pivot-cumsum(rev(Delta[-1]))))))
> Thanks for your attention!!!
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
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.
Received on Thu 17 Jun 2010 - 13:45:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jun 2010 - 13:50:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive