Skip to content
Prev 387577 / 398506 Next

Calculating column differences

Gerrit

 

Changed my approach ?

 

df <- data.frame(ID=1:5,Score=4*2:6)

 

df %>% 

  mutate(score_diff = Score - lag(Score, default = 0))

 

Jeff

 

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Gerrit Eichner
Sent: Wednesday, March 24, 2021 11:53 AM
To: r-help at r-project.org
Subject: Re: [R] Calculating column differences

 

Dear Jeff,

 

read diff's help page, and you'll find out what is wrong with your expectation.

 

What do think diff(df$Score) should give for the first element in df$Score??

 

  Hth  --  Gerrit

 

---------------------------------------------------------------------

Dr. Gerrit Eichner                   Mathematical Institute, Room 212

 <mailto:gerrit.eichner at math.uni-giessen.de> gerrit.eichner at math.uni-giessen.de   Justus-Liebig-University Giessen

Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany

 <http://www.uni-giessen.de/eichner> http://www.uni-giessen.de/eichner

---------------------------------------------------------------------



Am 24.03.2021 um 17:48 schrieb Jeff Reichman:

        

        

            
        

        

            
        

        

            
______________________________________________

 <mailto:R-help at r-project.org> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see  <https://stat.ethz.ch/mailman/listinfo/r-help> 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.