2017-10-13 216 views
-2

``[你好,我有这个数据框,我想计算R的名义gdp的增长率,我知道如何在excel中做公式( (今年的gdp) - 去年的gdp)/(去年的gdp))* 100,R可以用什么样的命令来计算它? ] [1]见图片[1]:https://i.stack.imgur.com/w7ral.png我怎样才能找到在R

年 2004年 2006年 2009年

名义GDP 7696034.9 8690254.3 9424601.9 10520792.8 11399472.2 12256863。 6 12072541.6 13266857.9 14527336.9 15599270.7 16078959.8

+0

不清楚你在问什么,因为在你的DF没有GDP – PoGibas

+1

@PoGibas其实,有被称为'PIB'在他的语言,西班牙语。 (在我的葡萄牙语中。) –

+0

做**不**将数据发布为图片,编辑您的问题并发布'dput(df)'的输出。 –

回答

1

您也可以从dplyr使用lag()机能的研究。它在向量中给出了以前的值。下面是一个例子

data <- data.frame(year = c(2003:2013), 
        gdp = c(7696034.9, 8690254.3, 9424601.9, 10520792.8, 
          11399472.2, 12256863.6, 12072541.6, 13266857.9, 
          14527336.9, 15599270.7, 16078959.8)) 
library(dplyr) 
growth_rate <- function(x)(x/lag(x)-1)*100 
data$growth_rate <- growth_rate(data$gdp) 
0

这也可能是最适合你熟悉的数据表,做这样的事情:

library(data.table) 

dt_gdp <- data.table(df) 

dt_gdp[, growth_rate_of_gdp := 100 * (Producto.interno.bruto..PIB. - shift(Producto.interno.bruto..PIB.))/shift(Producto.interno.bruto..PIB.)]