[R] difference
jim holtman
jholtman at gmail.com
Fri Oct 28 19:34:44 CEST 2016
I read the problem incorrectly; I did not see that you wanted the
difference from the first entry; trying again:
> 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 = Num - Num[1L])
Source: local data frame [6 x 3]
Groups: Year [2]
Year Num diff
<int> <int> <int>
1 2001 25 0
2 2001 75 50
3 2001 150 125
4 2002 30 0
5 2002 85 55
6 2002 95 65
>
> # use data.table
> require(data.table)
> setDT(input) # convert to data.table
> input[, diff := Num - Num[1L], by = Year][] # print output
Year Num diff
1: 2001 25 0
2: 2001 75 50
3: 2001 150 125
4: 2002 30 0
5: 2002 85 55
6: 2002 95 65
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