eu estou fazendo um trabalho de econometria e gostaria de tirar a primeira diferença da Paridade do Poder de compra para os países em que estou analisando. O problema é que a base está em formato long, agrupada para os países, mas agora não sei como posso realizar o diff condicionando as informações?
# Dados
library(readxl)
PPC.World.Bank <- read_excel("Desktop/FGV EESP/Econometria 2/Data_Extract_From_World_Development_Indicators.xlsx")
View(PPC.World.Bank)
#Pacotes
library(dplyr)
library(tidyverse)
library(ggplot2)
library(ggthemes)
library(reshape2)
#Filtragem e Organização dos dados
#Renomeando colunas
colnames(PPC.World.Bank) <- c(
"Series Name", "Series Code", "Country Name", "Country Code","1994","1995",
"1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006",
"2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017",
"2018","2019","2020","2021","2022")
# Reovendo linhas em branco
PPC.1 = slice(PPC.World.Bank, 1:4)
View(PPC.1)
#Formato long
PPC.long <-melt(PPC.1)
View(PPC.long)
#Renomeando colunas
colnames(PPC.long) <- c(
"Series Name", "Series Code", "Country Name", "Country Code","Ano", "Paridade do Poder de Compra")
Ano = PPC.long$Ano
PPC = PPC.long$`Paridade do Poder de Compra`
#Gráficos
graf.1.ppc = ggplot(data = PPC.long, mapping =aes(x = Ano, y = PPC, group = 1))+
geom_line(size = 0.5)+ #queremos um gráfico de linhas
geom_point(color = "Dark Red", size = 0.8) + facet_wrap(~PPC.long$`Country Name`, scales ='free_y') +
scale_x_discrete(breaks =c('1994','1999','2004','2009','2013','2017', '2021'))+
xlab('Ano')+
ylab('')+
ggtitle('Variação da Paridade do Poder de Compra na América Latina')+
theme_hc() +
theme(legend.key.width = unit(1, 'cm'))
#Gráfico Interativo
library(plotly)
graf.1.ppc <- ggplotly(graf.1.ppc)
graf.1.ppc
#Aplicando a primeira diferença (??????)
PPC.long$D.PPC <- c(diff(PPC.long$`Paridade do Poder de Compra`[PPC.long$`Country Name` == 'Brazil']))
#Dados
Country Name Country Code Ano Paridade do Poder de Compra
1 Brazil BRA 1994 0.5186519
2 Colombia COL 1994 416.0606291
3 Chile CHL 1994 318.0708270
4 Mexico MEX 1994 2.5233760
5 Brazil BRA 1995 0.8375032
6 Colombia COL 1995 489.2790238
7 Chile CHL 1995 332.0346800
8 Mexico MEX 1995 3.2874370
9 Brazil BRA 1996 0.9418661
10 Colombia COL 1996 574.2037233
11 Chile CHL 1996 337.8788510
12 Mexico MEX 1996 4.2009140
13 Brazil BRA 1997 0.9841012
14 Colombia COL 1997 664.6783632
15 Chile CHL 1997 344.2524390
16 Mexico MEX 1997 4.8611370
17 Brazil BRA 1998 1.0000209
18 Colombia COL 1998 776.7906741
19 Chile CHL 1998 353.3377630
20 Mexico MEX 1998 5.4995960
21 Brazil BRA 1999 1.0261538
22 Colombia COL 1999 842.8132316
23 Chile CHL 1999 357.2808230
24 Mexico MEX 1999 6.2246400
25 Brazil BRA 2000 1.0625565
26 Colombia COL 2000 890.4723000
27 Chile CHL 2000 363.4930230
28 Mexico MEX 2000 6.7508860
29 Brazil BRA 2001 1.1040372
30 Colombia COL 2001 935.0151684
31 Chile CHL 2001 373.5080360
32 Mexico MEX 2001 6.9152720
33 Brazil BRA 2002 1.1786367
34 Colombia COL 2002 978.8812648
35 Chile CHL 2002 381.0144050
36 Mexico MEX 2002 7.2375840
37 Brazil BRA 2003 1.3220601
38 Colombia COL 2003 1025.4050898
39 Chile CHL 2003 382.5364820
40 Mexico MEX 2003 7.3441190
41 Brazil BRA 2004 1.3725329
42 Colombia COL 2004 1057.6024091
43 Chile CHL 2004 380.3494520
44 Mexico MEX 2004 7.4658230
45 Brazil BRA 2005 1.4186871
46 Colombia COL 2005 1074.5689477
47 Chile CHL 2005 387.3600000
48 Mexico MEX 2005 7.6483310
49 Brazil BRA 2006 1.4318482
50 Colombia COL 2006 1085.6711396
51 Chile CHL 2006 338.4908630
52 Mexico MEX 2006 7.7403490
53 Brazil BRA 2007 1.4428266
54 Colombia COL 2007 1114.0917494
55 Chile CHL 2007 343.1078220
56 Mexico MEX 2007 7.9550070
57 Brazil BRA 2008 1.4683860
58 Colombia COL 2008 1147.9895518
59 Chile CHL 2008 360.3642160
60 Mexico MEX 2008 8.1585910
61 Brazil BRA 2009 1.5456568
62 Colombia COL 2009 1200.4853878
63 Chile CHL 2009 367.5612430
64 Mexico MEX 2009 8.4331810
65 Brazil BRA 2010 1.5973411
66 Colombia COL 2010 1207.9398723
67 Chile CHL 2010 373.2912300
68 Mexico MEX 2010 8.7250210
69 Brazil BRA 2011 1.6512214
70 Colombia COL 2011 1210.9931641
71 Chile CHL 2011 370.1987370
72 Mexico MEX 2011 8.9402120
73 Brazil BRA 2012 1.6626555
74 Colombia COL 2012 1189.5036621
75 Chile CHL 2012 391.5723640
76 Mexico MEX 2012 9.2234710
77 Brazil BRA 2013 1.7916766
78 Colombia COL 2013 1220.9112549
79 Chile CHL 2013 390.1613030
80 Mexico MEX 2013 9.1808920
81 Brazil BRA 2014 1.9007571
82 Colombia COL 2014 1249.0191650
83 Chile CHL 2014 410.6707420
84 Mexico MEX 2014 9.3536260
85 Brazil BRA 2015 2.0829582
86 Colombia COL 2015 1305.7772217
87 Chile CHL 2015 447.2666320
88 Mexico MEX 2015 9.4335910
89 Brazil BRA 2016 2.2490232
90 Colombia COL 2016 1403.3064550
91 Chile CHL 2016 452.9225690
92 Mexico MEX 2016 9.4599330
93 Brazil BRA 2017 2.3273771
94 Colombia COL 2017 1467.2812320
95 Chile CHL 2017 463.2314130
96 Mexico MEX 2017 10.1353830
97 Brazil BRA 2018 2.3551456
98 Colombia COL 2018 1464.4117980
99 Chile CHL 2018 458.3124900
100 Mexico MEX 2018 10.3514900
101 Brazil BRA 2019 2.3995772
102 Colombia COL 2019 1516.4399870
103 Chile CHL 2019 465.2358790
104 Mexico MEX 2019 10.7914090
105 Brazil BRA 2020 2.4464668
106 Colombia COL 2020 1529.9317530
107 Chile CHL 2020 469.0956380
108 Mexico MEX 2020 10.7418270
109 Brazil BRA 2021 2.5306766
110 Colombia COL 2021 1567.4466590
111 Chile CHL 2021 477.8180280
112 Mexico MEX 2021 11.0856900