Find change in percent of a table

Hi,

I have a table with data over import and export from the UK during 2010-2014. I am supposed to finde the relative change in percent of import to the year before from 2011 to 2016. Currently my code looks like this:

change2010_11 <- (df$import[2]-df$import[1])/df$import[1]*100

That gives me the change in import in percent from the year before. However, I am supposed to do this for 2011 to 2016, which means I need to write a couple of lines of code.
So what I am wondering, is if I can somehow specify that I want it from 2011 to 2016 instead of writing a code for each year.

Hope someone can help.

Thanks in advance!

What is the structure of your data? can you share about 10 rows in a minimal reprex?? This would help a lot. Guide here: https://www.jessemaegan.com/post/so-you-ve-been-asked-to-make-a-reprex/

Hi, thank you for replying. I am not sure if I have done it correctly. But here it is:

data.frame(
  stringsAsFactors = FALSE,
              year = c("2010", "2011", "2012", "2013", "2014", "2015"),
            import = c(28137L, 32295L, 29255L, 29732L, 26480L, 25664L),
            export = c(45033L, 58986L, 61325L, 52782L, 48684L, 39695L)
)
#>   year import export
#> 1 2010  28137  45033
#> 2 2011  32295  58986
#> 3 2012  29255  61325
#> 4 2013  29732  52782
#> 5 2014  26480  48684
#> 6 2015  25664  39695

It is also supposed to include the 2016 row, but somehow it didn't print when I used the head() function at the start. I guess I am supposed to use print() instead?

It looks like you did not include a 2016 row in this dataset.

Is the approach below (mutate to create a new column, use the lag() function to compare to the prior year) helpful?

library(tidyverse)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'

data <- data.frame(
  stringsAsFactors = FALSE,
  year = c("2010", "2011", "2012", "2013", "2014", "2015"),
  import = c(28137L, 32295L, 29255L, 29732L, 26480L, 25664L),
  export = c(45033L, 58986L, 61325L, 52782L, 48684L, 39695L)
)

data %>% 
  mutate(ch_import_pct = round((100*(import - lag(import))/lag(import)),2), 
        ch_export_pct = round((100*(export - lag(export))/lag(export)),2))
#>   year import export ch_import_pct ch_export_pct
#> 1 2010  28137  45033            NA            NA
#> 2 2011  32295  58986         14.78         30.98
#> 3 2012  29255  61325         -9.41          3.97
#> 4 2013  29732  52782          1.63        -13.93
#> 5 2014  26480  48684        -10.94         -7.76
#> 6 2015  25664  39695         -3.08        -18.46

Created on 2020-09-09 by the reprex package (v0.3.0)

Hey! Yeah when using the head() function the last row is not included. I don't know why...

However, the code you sent me worked perfectly! Thank you very much for your time and help :slight_smile:

1 Like

I think the default for head() is n=6
If needed, you can set the n as an argument, like
head(iris, n=12)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.