Hi R mailing list, probably a very basic problem here, I try to do the following:> Q<-c(1,2,3) > P<-c(4,5,6) > A<- data.frame(Q,P) > AQ P 1 1 4 2 2 5 3 3 6 this is my simplified data.frame (matrix) now I try to create following loop for subtraction of element within the data.frame:> for(i in length(A[,"P"]-1){delta[i]<- A[i,"P"]-A[i+1,"P"] } All I get is a vector of the correct length but with no readings. Thanks for any help on this. -- Niklaus Hürlimann Université de Lausanne Institut de Minéralogie et Géochimie L'Anthropole CH-1015 Lausanne Suisse E-mail: Niklaus.Hurlimann@unil.ch Tel:+41(0)21 692 4452 [[alternative HTML version deleted]]
Try this: Reduce("-", as.data.frame(embed(A$P, 2))) On Tue, Mar 30, 2010 at 10:15 AM, Niklaus Hurlimann <niklaus.hurlimann at unil.ch> wrote:> Hi R mailing list, > > probably a very basic problem here, I try to do the following: > >> Q<-c(1,2,3) >> P<-c(4,5,6) >> A<- data.frame(Q,P) >> A > ?Q P > 1 1 4 > 2 2 5 > 3 3 6 > > this is my simplified data.frame (matrix) now I try to create following > loop for subtraction of element within the data.frame: > >> for(i in length(A[,"P"]-1){ > ?delta[i]<- A[i,"P"]-A[i+1,"P"] > } > > All I get is a vector of ?the correct length but with no readings. > > Thanks for any help on this. > > > > -- > Niklaus H?rlimann > > Universit? de Lausanne > Institut de Min?ralogie et G?ochimie > L'Anthropole > CH-1015 Lausanne > Suisse > > E-mail: Niklaus.Hurlimann at unil.ch > Tel:+41(0)21 692 4452 > > ? ? ? ?[[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. > >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Perhaps you're just looking for the diff() function? See ?diff. -Peter Ehlers On 2010-03-30 7:15, Niklaus Hurlimann wrote:> Hi R mailing list, > > probably a very basic problem here, I try to do the following: > >> Q<-c(1,2,3) >> P<-c(4,5,6) >> A<- data.frame(Q,P) >> A > Q P > 1 1 4 > 2 2 5 > 3 3 6 > > this is my simplified data.frame (matrix) now I try to create following > loop for subtraction of element within the data.frame: > >> for(i in length(A[,"P"]-1){ > delta[i]<- A[i,"P"]-A[i+1,"P"] > } > > All I get is a vector of the correct length but with no readings. > > Thanks for any help on this. > > > > > > > ______________________________________________ > 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.-- Peter Ehlers University of Calgary
Maybe Matching Threads
- Fitting a half-ellipse curve
- Error "singular gradient matrix at initial parameter estimates" in nls
- plotting density in same plot in loop iteration
- Restore a node in a replicating Gluster setup after data loss
- What does "smaller than" comparison do on strings?