[R] difference of two rows

jim holtman jholtman at gmail.com
Wed Nov 25 18:50:19 CET 2009


Try this:

> x <- read.table(textConnection("ID YEAR
+ 13 2007
+ 15 2003
+ 15 2006
+ 15 2008
+ 21 2006
+ 21 2007"), header=TRUE)
> x$diff <- ave(x$YEAR, x$ID, FUN=function(a) c(diff(a), NA))
>
> x
  ID YEAR diff
1 13 2007   NA
2 15 2003    3
3 15 2006    2
4 15 2008   NA
5 21 2006    1
6 21 2007   NA


On Wed, Nov 25, 2009 at 10:55 AM, clion <birte_2 at hotmail.com> wrote:
>
> Dear R user,
> I'd like to calculate the difference of two rows, where "ID" is the same.
> eg.: I've got the following dataframe:
> ID YEAR
> 13 2007
> 15 2003
> 15 2006
> 15 2008
> 21 2006
> 21 2007
>
> and I'd like to get the difference, like this:
> ID YEAR     diff
> 13 2007      NA
> 15 2003       3
> 15 2006       2
> 15 2008      NA
> 21 2006       1
> 21 2007      NA
>
> that should be fairly easy...I hope
> Thanks for any helpful comments
> B.
>
>
>
> --
> View this message in context: http://old.nabble.com/difference-of-two-rows-tp26515212p26515212.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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