?diff
> x
ID LENGTH DIFF
1 1 10 NA
2 2 15 5
3 3 20 5
4 4 12 -8
5 5 18 6> x <- x[,-3]
> x
ID LENGTH
1 1 10
2 2 15
3 3 20
4 4 12
5 5 18> x$DIFF <- c(NA, diff(x$LENGTH) )
> x
ID LENGTH DIFF
1 1 10 NA
2 2 15 5
3 3 20 5
4 4 12 -8
5 5 18 6
On Tue, May 5, 2009 at 7:00 PM, Mark Na <mtb954@gmail.com> wrote:
> Dear R-helpers,
>
> I have a dataframe with several columns, one of which is called LENGTH.
>
> I would like to make a new column called DIFF containing the value of
> LENGTH minus LENGTH in the previous row, like this:
>
> ID LENGTH DIFF
> 1 1 10 NA
> 2 2 15 5
> 3 3 20 5
> 4 4 12 -8
> 5 5 18 6
>
> I'd like to think there are "relative subscripts" in R but I
can't
> find any reference to such a thing.
>
> Any help solving this problem would be much appreciated, thanks!
>
> Mark Na
>
> ______________________________________________
> R-help@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<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?
[[alternative HTML version deleted]]