Calculating column differences

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating column differences

reichmaj
r-help forum

 

I'm trying to calculate the diff between two rows and them mutate the
difference into a new column. I'm using the diff function but not giving me
what I want.

 

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

 

What a want  where

  ID Score  diff

1  1     8      8

2  2    12     4

3  3    16     4

4  4    20     4

5  5    24     4

 

What I am getting

  ID Score  diff

1  1     8      NA

2  2    12     4

3  3    16     4

4  4    20     4

5  5    24     4

 

Jeff

 


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating column differences

Gerrit Eichner
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
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 24.03.2021 um 17:48 schrieb Jeff Reichman:

> r-help forum
>
>  
>
> I'm trying to calculate the diff between two rows and them mutate the
> difference into a new column. I'm using the diff function but not giving me
> what I want.
>
>  
>
> df <- data.frame(ID=1:5,Score=4*2:6)
>
>  
>
> What a want  where
>
>    ID Score  diff
>
> 1  1     8      8
>
> 2  2    12     4
>
> 3  3    16     4
>
> 4  4    20     4
>
> 5  5    24     4
>
>  
>
> What I am getting
>
>    ID Score  diff
>
> 1  1     8      NA
>
> 2  2    12     4
>
> 3  3    16     4
>
> 4  4    20     4
>
> 5  5    24     4
>
>  
>
> Jeff
>
>  
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating column differences

R help mailing list-2
In reply to this post by reichmaj
Dear Jeff,

Rather than diff-ing a linear vector you're trying to diff values from
two different rows. Also you indicate that you want to place the
diff-ed value in the 'lower' row of a new column. Try this (note
insertion of an initial "zero" row):

> df <- data.frame(ID=1:5,Score=4*2:6)
> df1 <- rbind(c(0,0), df)
> cbind(df1, "diff"=c(0, diff(df1$Score)) )
  ID Score diff
1  0     0    0
2  1     8    8
3  2    12    4
4  3    16    4
5  4    20    4
6  5    24    4
>

HTH, Bill.

W. Michels, Ph.D.



On Wed, Mar 24, 2021 at 9:49 AM Jeff Reichman <[hidden email]> wrote:

>
> r-help forum
>
>
>
> I'm trying to calculate the diff between two rows and them mutate the
> difference into a new column. I'm using the diff function but not giving me
> what I want.
>
>
>
> df <- data.frame(ID=1:5,Score=4*2:6)
>
>
>
> What a want  where
>
>   ID Score  diff
>
> 1  1     8      8
>
> 2  2    12     4
>
> 3  3    16     4
>
> 4  4    20     4
>
> 5  5    24     4
>
>
>
> What I am getting
>
>   ID Score  diff
>
> 1  1     8      NA
>
> 2  2    12     4
>
> 3  3    16     4
>
> 4  4    20     4
>
> 5  5    24     4
>
>
>
> Jeff
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating column differences

reichmaj
In reply to this post by Gerrit Eichner
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 <[hidden email]> On Behalf Of Gerrit Eichner
Sent: Wednesday, March 24, 2021 11:53 AM
To: [hidden email]
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:[hidden email]> [hidden email]   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:

> r-help forum

>

>  

>

> I'm trying to calculate the diff between two rows and them mutate the

> difference into a new column. I'm using the diff function but not

> giving me what I want.

>

>  

>

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

>

>  

>

> What a want  where

>

>    ID Score  diff

>

> 1  1     8      8

>

> 2  2    12     4

>

> 3  3    16     4

>

> 4  4    20     4

>

> 5  5    24     4

>

>  

>

> What I am getting

>

>    ID Score  diff

>

> 1  1     8      NA

>

> 2  2    12     4

>

> 3  3    16     4

>

> 4  4    20     4

>

> 5  5    24     4

>

>  

>

> Jeff

>

>  

>

>

>             [[alternative HTML version deleted]]

>

> ______________________________________________

>  <mailto:[hidden email]> [hidden email] 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.

>



______________________________________________

 <mailto:[hidden email]> [hidden email] 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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating column differences

R help mailing list-2
In reply to this post by R help mailing list-2
More correctly, with an initial "NA" value in the "diff" column:

> df <- data.frame(ID=1:5,Score=4*2:6)
> df1 <- rbind(c(0,0), df)
> cbind(df1, "diff"=c(NA, diff(df1$Score)) )
  ID Score diff
1  0     0   NA
2  1     8    8
3  2    12    4
4  3    16    4
5  4    20    4
6  5    24    4
>

HTH, Bill.
On Wed, Mar 24, 2021 at 10:59 AM William Michels <[hidden email]> wrote:

>
> Dear Jeff,
>
> Rather than diff-ing a linear vector you're trying to diff values from
> two different rows. Also you indicate that you want to place the
> diff-ed value in the 'lower' row of a new column. Try this (note
> insertion of an initial "zero" row):
>
> > df <- data.frame(ID=1:5,Score=4*2:6)
> > df1 <- rbind(c(0,0), df)
> > cbind(df1, "diff"=c(0, diff(df1$Score)) )
>   ID Score diff
> 1  0     0    0
> 2  1     8    8
> 3  2    12    4
> 4  3    16    4
> 5  4    20    4
> 6  5    24    4
> >
>
> HTH, Bill.
>
> W. Michels, Ph.D.
>
>
>
> On Wed, Mar 24, 2021 at 9:49 AM Jeff Reichman <[hidden email]> wrote:
> >
> > r-help forum
> >
> >
> >
> > I'm trying to calculate the diff between two rows and them mutate the
> > difference into a new column. I'm using the diff function but not giving me
> > what I want.
> >
> >
> >
> > df <- data.frame(ID=1:5,Score=4*2:6)
> >
> >
> >
> > What a want  where
> >
> >   ID Score  diff
> >
> > 1  1     8      8
> >
> > 2  2    12     4
> >
> > 3  3    16     4
> >
> > 4  4    20     4
> >
> > 5  5    24     4
> >
> >
> >
> > What I am getting
> >
> >   ID Score  diff
> >
> > 1  1     8      NA
> >
> > 2  2    12     4
> >
> > 3  3    16     4
> >
> > 4  4    20     4
> >
> > 5  5    24     4
> >
> >
> >
> > Jeff
> >
> >
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating column differences

reichmaj
Bill

I ended up taking a different approach
miDat <- miDat %>%
  mutate(new_cases = cases - lag(cases, default = 0))

 - or -

df <- df %>%
  mutate(diff = Score - lag(Score, default = 0))

Jeff

-----Original Message-----
From: William Michels <[hidden email]>
Sent: Wednesday, March 24, 2021 1:41 PM
To: [hidden email]
Cc: [hidden email]; Gerrit Eichner <[hidden email]>
Subject: Re: [R] Calculating column differences

More correctly, with an initial "NA" value in the "diff" column:

> df <- data.frame(ID=1:5,Score=4*2:6)
> df1 <- rbind(c(0,0), df)
> cbind(df1, "diff"=c(NA, diff(df1$Score)) )
  ID Score diff
1  0     0   NA
2  1     8    8
3  2    12    4
4  3    16    4
5  4    20    4
6  5    24    4
>

HTH, Bill.
On Wed, Mar 24, 2021 at 10:59 AM William Michels <[hidden email]> wrote:

>
> Dear Jeff,
>
> Rather than diff-ing a linear vector you're trying to diff values from
> two different rows. Also you indicate that you want to place the
> diff-ed value in the 'lower' row of a new column. Try this (note
> insertion of an initial "zero" row):
>
> > df <- data.frame(ID=1:5,Score=4*2:6)
> > df1 <- rbind(c(0,0), df)
> > cbind(df1, "diff"=c(0, diff(df1$Score)) )
>   ID Score diff
> 1  0     0    0
> 2  1     8    8
> 3  2    12    4
> 4  3    16    4
> 5  4    20    4
> 6  5    24    4
> >
>
> HTH, Bill.
>
> W. Michels, Ph.D.
>
>
>
> On Wed, Mar 24, 2021 at 9:49 AM Jeff Reichman <[hidden email]> wrote:
> >
> > r-help forum
> >
> >
> >
> > I'm trying to calculate the diff between two rows and them mutate
> > the difference into a new column. I'm using the diff function but
> > not giving me what I want.
> >
> >
> >
> > df <- data.frame(ID=1:5,Score=4*2:6)
> >
> >
> >
> > What a want  where
> >
> >   ID Score  diff
> >
> > 1  1     8      8
> >
> > 2  2    12     4
> >
> > 3  3    16     4
> >
> > 4  4    20     4
> >
> > 5  5    24     4
> >
> >
> >
> > What I am getting
> >
> >   ID Score  diff
> >
> > 1  1     8      NA
> >
> > 2  2    12     4
> >
> > 3  3    16     4
> >
> > 4  4    20     4
> >
> > 5  5    24     4
> >
> >
> >
> > Jeff
> >
> >
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.