按月计算的差异 (dplyr, R)
Difference by month (dplyr, R)
假设我有一个如下所示的数据集。文本重复 'a' 到 'e' 每个月都有值。时间为2016年1月至2016年3月。
date text value
1-16 a 13
1-16 b 2
1-16 c 3
1-16 d 1
1-16 e 20
2-16 a 30
2-16 b 50
2-16 c 20
2-16 d 10
2-16 e 40
3-16 a 34
3-16 b 3
3-16 c 2
3-16 d 1
3-16 e 4
我想制作一个按月和按文本具有差异值的列。我的意思是,17 = (30(a, 2-16) - 13(a, 1-16)) 这样,
date text value the value that I want to have
1-16 a 13 na
1-16 b 2 na
1-16 c 3 na
1-16 d 1 na
1-16 e 20 na
2-16 a 30 =(value(a, 2016-feb) - value(a, 2016-jan)) 17
2-16 b 50 =(value(b, 2016-feb) - value(b, 2016-jan)) 48
2-16 c 20 =(value(c, 2016-feb) - value(c, 2016-jan)) 17
2-16 d 10 =(value(d, 2016-feb) - value(d, 2016-jan)) 9
2-16 e 40 =(value(e, 2016-feb) - value(e, 2016-jan)) 20
3-16 a 34 =(value(a, 2016-mar) - value(a, 2016-feb)) 4
3-16 b 3 =(value(b, 2016-mar) - value(b, 2016-feb)) -47
3-16 c 2 =(value(c, 2016-mar) - value(c, 2016-feb)) -18
3-16 d 1 =(value(d, 2016-mar) - value(d, 2016-feb)) -9
3-16 e 4 =(value(e, 2016-mar) - value(e, 2016-feb)) -36
上面的数据是我做的。实际数据更长甚至更复杂,因为它包含每个月大约 2000 条文本,而且数据甚至不是最新的。 (也分很多类)。所以一些手动计算值的解决方案是行不通的。
我试过 dplyr 滞后,例如,
df %>% group_by(date, text) %>%
arrange(date, text) %>%
mutate(diff = value - lag(value))
但是没有用。我假设代码没有很好地识别文本?
最好的方法是什么?
与dplyr
...
library(dplyr)
df %>% group_by(text) %>% mutate(newval=c(NA,diff(value)))
date text value newval
<chr> <chr> <int> <int>
1 1-16 a 13 NA
2 1-16 b 2 NA
3 1-16 c 3 NA
4 1-16 d 1 NA
5 1-16 e 20 NA
6 2-16 a 30 17
7 2-16 b 50 48
8 2-16 c 20 17
9 2-16 d 10 9
10 2-16 e 40 20
11 3-16 a 34 4
12 3-16 b 3 -47
13 3-16 c 2 -18
14 3-16 d 1 -9
15 3-16 e 4 -36
或完全在 R 基础中使用 ave
> df$newVal <- ave(df$value, df$text, FUN=function(x) c(NA,diff(x)))
> df
date text value newVal
1 1-16 a 13 NA
2 1-16 b 2 NA
3 1-16 c 3 NA
4 1-16 d 1 NA
5 1-16 e 20 NA
6 2-16 a 30 17
7 2-16 b 50 48
8 2-16 c 20 17
9 2-16 d 10 9
10 2-16 e 40 20
11 3-16 a 34 4
12 3-16 b 3 -47
13 3-16 c 2 -18
14 3-16 d 1 -9
15 3-16 e 4 -36
和aggregate
df$newval <- c(aggregate(value ~ text, data=df, FUN=function(x) c(NA,diff(x)))[,-1])
假设我有一个如下所示的数据集。文本重复 'a' 到 'e' 每个月都有值。时间为2016年1月至2016年3月。
date text value
1-16 a 13
1-16 b 2
1-16 c 3
1-16 d 1
1-16 e 20
2-16 a 30
2-16 b 50
2-16 c 20
2-16 d 10
2-16 e 40
3-16 a 34
3-16 b 3
3-16 c 2
3-16 d 1
3-16 e 4
我想制作一个按月和按文本具有差异值的列。我的意思是,17 = (30(a, 2-16) - 13(a, 1-16)) 这样,
date text value the value that I want to have
1-16 a 13 na
1-16 b 2 na
1-16 c 3 na
1-16 d 1 na
1-16 e 20 na
2-16 a 30 =(value(a, 2016-feb) - value(a, 2016-jan)) 17
2-16 b 50 =(value(b, 2016-feb) - value(b, 2016-jan)) 48
2-16 c 20 =(value(c, 2016-feb) - value(c, 2016-jan)) 17
2-16 d 10 =(value(d, 2016-feb) - value(d, 2016-jan)) 9
2-16 e 40 =(value(e, 2016-feb) - value(e, 2016-jan)) 20
3-16 a 34 =(value(a, 2016-mar) - value(a, 2016-feb)) 4
3-16 b 3 =(value(b, 2016-mar) - value(b, 2016-feb)) -47
3-16 c 2 =(value(c, 2016-mar) - value(c, 2016-feb)) -18
3-16 d 1 =(value(d, 2016-mar) - value(d, 2016-feb)) -9
3-16 e 4 =(value(e, 2016-mar) - value(e, 2016-feb)) -36
上面的数据是我做的。实际数据更长甚至更复杂,因为它包含每个月大约 2000 条文本,而且数据甚至不是最新的。 (也分很多类)。所以一些手动计算值的解决方案是行不通的。
我试过 dplyr 滞后,例如,
df %>% group_by(date, text) %>%
arrange(date, text) %>%
mutate(diff = value - lag(value))
但是没有用。我假设代码没有很好地识别文本?
最好的方法是什么?
与dplyr
...
library(dplyr)
df %>% group_by(text) %>% mutate(newval=c(NA,diff(value)))
date text value newval
<chr> <chr> <int> <int>
1 1-16 a 13 NA
2 1-16 b 2 NA
3 1-16 c 3 NA
4 1-16 d 1 NA
5 1-16 e 20 NA
6 2-16 a 30 17
7 2-16 b 50 48
8 2-16 c 20 17
9 2-16 d 10 9
10 2-16 e 40 20
11 3-16 a 34 4
12 3-16 b 3 -47
13 3-16 c 2 -18
14 3-16 d 1 -9
15 3-16 e 4 -36
或完全在 R 基础中使用 ave
> df$newVal <- ave(df$value, df$text, FUN=function(x) c(NA,diff(x)))
> df
date text value newVal
1 1-16 a 13 NA
2 1-16 b 2 NA
3 1-16 c 3 NA
4 1-16 d 1 NA
5 1-16 e 20 NA
6 2-16 a 30 17
7 2-16 b 50 48
8 2-16 c 20 17
9 2-16 d 10 9
10 2-16 e 40 20
11 3-16 a 34 4
12 3-16 b 3 -47
13 3-16 c 2 -18
14 3-16 d 1 -9
15 3-16 e 4 -36
和aggregate
df$newval <- c(aggregate(value ~ text, data=df, FUN=function(x) c(NA,diff(x)))[,-1])