r-使用数据帧列中的下一个非na值进行计算

时间:2018-08-16 15:54:01

标签: r lag lead

我在一个数据框中有一些数据,我想计算month值之间的百分比变化。问题是我在某些条目中有NA,并且引发了计算。

       irm     code        price    pct.change
1  201807 511130F075A04      4.6600   2.192982
2  201806 511130F075A04      4.5600   1.333333
3  201805 511130F075A04      4.5000 -13.461538
4  201804 511130F075A04      5.2000         NA
5  201803 511130F075A04          NA         NA
6  201802 511130F075A04      4.9100   1.867220
7  201801 511130F075A04      4.8200  -5.304519
8  201712 511130F075A04      5.0900   2.414487
9  201711 511130F075A04      4.9700  -3.307393
10 201710 511130F075A04      5.1400         NA
11 201709 511130F075A04          NA         NA
12 201708 511130F075A04      5.2900   2.918288
13 201707 511130F075A04      5.1400  66.553255
14 201706 511130F075A04      3.0861 -10.664351
15 201705 511130F075A04      3.4545  -7.241824

问题出在pct.change列的第4行和第10行。它们是NA,但我希望使用price而不是NA的最新值来计算它们。所需的输出将是(请参阅第4和10行):

       irm     code        price    pct.change
1  201807 511130F075A04      4.6600   2.192982
2  201806 511130F075A04      4.5600   1.333333
3  201805 511130F075A04      4.5000 -13.461538
**4  201804 511130F075A04      5.2000   5.906314**
5  201803 511130F075A04          NA         NA
6  201802 511130F075A04      4.9100   1.867220
7  201801 511130F075A04      4.8200  -5.304519
8  201712 511130F075A04      5.0900   2.414487
9  201711 511130F075A04      4.9700  -3.307393
**10 201710 511130F075A04      5.1400  -2.835539**
11 201709 511130F075A04          NA         NA
12 201708 511130F075A04      5.2900   2.918288
13 201707 511130F075A04      5.1400  66.553255
14 201706 511130F075A04      3.0861 -10.664351
15 201705 511130F075A04      3.4545  -7.241824

我尝试使用标准(x/lead(x) - 1)*100和一些使用(x/lag(which(!is.na(lead(x))的变体,但似乎缺少一些东西。是否有一种简单的方法可以在base甚至dplyr中完成? 我不想替换NA,我想保留它们。

2 个答案:

答案 0 :(得分:2)

@LAP的评论可能是最好的方法。 data.table

的语法要好一些
library(data.table)
setDT(df)

df[!is.na(price), pct.change := 100*(price/shift(price, type = 'lead') - 1)]

#        irm          code  price pct.change
#  1: 201807 511130F075A04 4.6600   2.192982
#  2: 201806 511130F075A04 4.5600   1.333333
#  3: 201805 511130F075A04 4.5000 -13.461538
#  4: 201804 511130F075A04 5.2000   5.906314
#  5: 201803 511130F075A04     NA         NA
#  6: 201802 511130F075A04 4.9100   1.867220
#  7: 201801 511130F075A04 4.8200  -5.304519
#  8: 201712 511130F075A04 5.0900   2.414487
#  9: 201711 511130F075A04 4.9700  -3.307393
# 10: 201710 511130F075A04 5.1400  -2.835539
# 11: 201709 511130F075A04     NA         NA
# 12: 201708 511130F075A04 5.2900   2.918288
# 13: 201707 511130F075A04 5.1400  66.553255
# 14: 201706 511130F075A04 3.0861 -10.664351
# 15: 201705 511130F075A04 3.4545         NA

答案 1 :(得分:1)

在Base R中,您可以决定替换:

import os
import sys
sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('../../'))


project = 'test'
copyright = ''
author = ''

version = ''
release = '0'

extensions = [
    'sphinx.ext.autodoc',
]

source_suffix = '.rst'
master_doc = 'index'