[R] difference

jim holtman jholtman at gmail.com
Fri Oct 28 19:32:40 CEST 2016


Here are a couple of other ways using 'dplyr' and 'data.table'

> require(dplyr)
> input <- read.table(text = "Year   Num
+ 2001    25
+ 2001    75
+ 2001   150
+ 2002    30
+ 2002    85
+ 2002    95", header = TRUE)
>
> input %>%
+     group_by(Year) %>%
+     mutate(diff = c(0, diff(Num)))
Source: local data frame [6 x 3]
Groups: Year [2]

   Year   Num  diff
  <int> <int> <dbl>
1  2001    25     0
2  2001    75    50
3  2001   150    75
4  2002    30     0
5  2002    85    55
6  2002    95    10
>
> # use data.table
> require(data.table)
Loading required package: data.table
data.table 1.9.6  For help type ?data.table or
https://github.com/Rdatatable/data.table/wiki
The fastest way to learn (by data.table authors):
https://www.datacamp.com/courses/data-analysis-the-data-table-way
-------------------------------------------------------------------------------------------------------
data.table + dplyr code now lives in dtplyr.
Please library(dtplyr)!
-------------------------------------------------------------------------------------------------------

Attaching package: ‘data.table’

The following objects are masked from ‘package:dplyr’:

    between, last

> setDT(input)  # convert to data.table
> input[, diff := c(0, diff(Num)), by = Year][]  # print output
   Year Num diff
1: 2001  25    0
2: 2001  75   50
3: 2001 150   75
4: 2002  30    0
5: 2002  85   55
6: 2002  95   10
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Fri, Oct 28, 2016 at 12:20 AM, Ashta <sewashm at gmail.com> wrote:
> Hi all,
>
> I want to calculate the difference  between successive row values to
> the first row value within year.
> How do I get that?
>
>  Here is    the sample of data
> Year   Num
> 2001    25
> 2001    75
> 2001   150
> 2002    30
> 2002    85
> 2002    95
>
> Desired output
> Year   Num  diff
> 2001    25       0
> 2001    75      50
> 2001  150    125
> 2002    30        0
> 2002    85      55
> 2002    95      65
>
> Thank you.
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list