2
votes

I haven't coded for several months and now am stuck with the following issue. I have the following dataset:

    Year World_export China_exp World_import China_imp
1  1992     3445.534   27.7310     3402.505    6.2220
2  1993     1940.061   27.8800     2474.038   18.3560
3  1994     2458.337   39.6970     2978.314    3.3270
4  1995     4641.168   15.9790     5504.787   18.0130
5  1996     5680.688   74.1650     6939.291   25.1870
6  1997     7206.604   70.2440     8639.422   31.9030
7  1998     7069.725   99.6510     8530.293   41.5030
8  1999     5916.077  169.4593     6673.743   37.8139
9  2000     7331.588  136.2180     8646.253   47.3789
10 2001     7471.374  143.0542     8292.893   41.2899
11 2002     8074.975  217.4286     9092.341   46.4730
12 2003     9956.433  162.2522    11558.007   71.7753
13 2004    13751.671  282.8678    16345.452  157.0768
14 2005    15976.238  430.8655    16708.094  284.1065
15 2006    19728.935  398.6704    22344.856  553.6356
16 2007    24275.244  484.5276    28693.113  815.7914
17 2008    32570.781  613.3714    39381.251 1414.8120
18 2009    21282.228  173.9463    28563.576 1081.3720
19 2010    25283.462  475.7635    34884.450 1684.0839
20 2011    41418.670  636.5881    45759.051 2193.8573
21 2012    46027.529  432.6025    46404.382 2373.4535
22 2013    37132.301  460.7133    43022.550 2829.3705
23 2014    36046.461  640.2552    40502.268 2373.2351
24 2015    26618.982  781.0016    30264.299 2401.1907
25 2016    23537.354  472.7022    27609.884 2129.4806

What i need is simple: to compute growth rates of each variable, that is, find difference between two elements, divide it by first element and multiply by 100.

I'm trying to write a script, that ends up with error message:

trade_Ch %>%  
  mutate ( 
        World_exp_grate = sapply(2:nrow(trade_Ch),function(i)((World_export[i]-World_export[i-1])/World_export[i-1]))
        )

Error in mutate_impl(.data, dots) : Column World_exp_grate must be length 25 (the number of rows) or one, not 24

although this piece of code gives me right values:

x <- sapply(2:nrow(trade_Ch),function(i)((trade_Ch$World_export[i]-trade_Ch$World_export[i-1])/trade_Ch$World_export[i-1]))  

So, generally speaking, I'd appreciate any support in correctly embedding the code into my MUTATE part from dplyr package.

OR

If there is another elegant way to solve this issue, would be grateful for any suggestions.

3
try trace_Ch %>% mutate(world_exp_grate = (World_export - lag(World_export)/lag(World_explort)) - akrun

3 Answers

1
votes

We can do

trade_Ch %>%
    mutate(world_exp_grate = 100*(World_export - lag(World_export))/lag(World_export))
1
votes

Hope this helps!

library(dplyr)
df %>%
  mutate_each(funs(chg = ((.-lag(.))/lag(.))*100), World_export:China_imp)
0
votes

The problem is that you cannot calculate the World_exp_grate for your first row. Therefore you have to set it to NA.

One variant to solve this is

trade_Ch %>%  
  mutate (World_export_lag = lag(World_export),
          World_exp_grate = (World_export - World_export_lag)/World_export_lag)) %>% 
  select(-World_export_lag)

lag shifts the vector by one position.

lag(1:5)

# [1] NA  1  2  3  4