我有一个数据框,如下所示:
origdate <- c(rep('2011-01-01',5), rep('2011-02-01',4), rep('2011-03-01',3))
date <- c('2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01',
'2011-05-01', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-01',
'2011-03-01', '2011-04-01', '2011-05-01')
A <- data.frame(origdate, date, bal=20:31)
A$origdate <- as.Date(A$origdate)
A$date <- as.Date(A$date)
A
origdate date bal
1 2011-01-01 2011-01-01 20
2 2011-01-01 2011-02-01 21
3 2011-01-01 2011-03-01 22
4 2011-01-01 2011-04-01 23
5 2011-01-01 2011-05-01 24
6 2011-02-01 2011-02-01 25
7 2011-02-01 2011-03-01 26
8 2011-02-01 2011-04-01 27
9 2011-02-01 2011-05-01 28
10 2011-03-01 2011-03-01 29
11 2011-03-01 2011-04-01 30
12 2011-03-01 2011-05-01 31
我想要做的是将bal
除以bal
的每个增量date
,而不是origdate
更改时的dbal
。所以我想要获得的内容显示在下面的 origdate date bal dbal
1 2011-01-01 2011-01-01 20 NA
2 2011-01-01 2011-02-01 21 1.050000
3 2011-01-01 2011-03-01 22 1.047619
4 2011-01-01 2011-04-01 23 1.045455
5 2011-01-01 2011-05-01 24 1.043478
6 2011-02-01 2011-02-01 25 NA
7 2011-02-01 2011-03-01 26 1.040000
8 2011-02-01 2011-04-01 27 1.038462
9 2011-02-01 2011-05-01 28 1.037037
10 2011-03-01 2011-03-01 29 NA
11 2011-03-01 2011-04-01 30 1.034483
12 2011-03-01 2011-05-01 31 1.033333
列中:
R
我可以编写一个复杂的双循环来执行此操作,但在<a href="https://www.google.com/">Google.com </a>
中有更简单的方法吗?
答案 0 :(得分:2)
使用dplyr
:
library(dplyr)
A %>% group_by(origdate) %>%
mutate(dbal = bal / lag(bal))
# Source: local data frame [12 x 4]
# Groups: origdate [3]
#
# origdate date bal dbal
# <date> <date> <int> <dbl>
# 1 2011-01-01 2011-01-01 20 NA
# 2 2011-01-01 2011-02-01 21 1.050000
# 3 2011-01-01 2011-03-01 22 1.047619
# 4 2011-01-01 2011-04-01 23 1.045455
# 5 2011-01-01 2011-05-01 24 1.043478
# 6 2011-02-01 2011-02-01 25 NA
# 7 2011-02-01 2011-03-01 26 1.040000
# 8 2011-02-01 2011-04-01 27 1.038462
# 9 2011-02-01 2011-05-01 28 1.037037
# 10 2011-03-01 2011-03-01 29 NA
# 11 2011-03-01 2011-04-01 30 1.034483
# 12 2011-03-01 2011-05-01 31 1.033333