Re: [R] difference of two rows

2009-11-26 Thread clion
Yes, thanks, that works perfectly! great command b. jholtman wrote: > > 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 >

Re: [R] difference of two rows

2009-11-25 Thread jim holtman
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 20033 3 15 20062 4 15 2008 NA 5 21 20061 6 21 2007

Re: [R] difference of two rows

2009-11-25 Thread R Help
You want to use tapply ?tapply This is a simple example dat = data.frame(a=sample(1:10,100,T),b=rnorm(100,0,1)) tapply(dat$b,dat$a,mean) Hope that helps, Sam On Wed, Nov 25, 2009 at 11:55 AM, clion wrote: > > Dear R user, > I'd like to calculate the difference of two rows, where "ID" is the s

[R] difference of two rows

2009-11-25 Thread clion
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