Dear all, I have a matrix with data and I want to substract from every value the previous element. Let's assume that my vector(matrix) is c<-(1,2,3,4,5) I want to get remove_previous c<-(0,1,2,3,4). How I can do that efficiently in R? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-09 15:34 UTC
[R] Substract "previous" element
Take a look at diff() though offhand I dont know what it does to matrices M On Oct 9, 2011, at 11:00 AM, Alaios <alaios at yahoo.com> wrote:> Dear all, > I have a matrix with data and I want to substract from every value the previous element. > > Let's assume that my vector(matrix) is c<-(1,2,3,4,5) > I want to get remove_previous c<-(0,1,2,3,4). > > How I can do that efficiently in R? > > I would like to thank you in advance for your help > B.R > Alex > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Vectors are not matrices in R, though matrices are special cases of vectors. See ?diff for a solution that works with vectors. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Alaios <alaios@yahoo.com> wrote: Dear all, I have a matrix with data and I want to substract from every value the previous element. Let's assume that my vector(matrix) is c<-(1,2,3,4,5) I want to get remove_previous c<-(0,1,2,3,4). How I can do that efficiently in R? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]] _____________________________________________ 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 and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]