title: How to interpolate/extrapolate using na.approx function within individual groups in R
body: <p>I have a panel dataset with 10 variables for 60 countries, across 18 years (2000-2017), and I have a lot of missing data. </p>
  
  <pre><code>Country Year    Broadband
  
  Albania 2000    NA
  Albania 2001    NA
  Albania 2002    NA
  Albania 2003    NA
  Albania 2004    NA
  Albania 2005    272
  Albania 2006    NA
  Albania 2007    10000
  Albania 2008    64000
  Albania 2009    92000
  Albania 2010    105539
  Albania 2011    128210
  Albania 2012    160088
  Albania 2013    182556
  Albania 2014    207931
  Albania 2015    242870
  Albania 2016    263874
  Albania 2017    NA
  Algeria 2000    NA
  Algeria 2001    NA
  Algeria 2002    NA
  Algeria 2003    18000
  Algeria 2004    36000
  </code></pre>
  
  <p>I would like to interpolate using the na.approx function in R (and extrapolate using rule = 2), but only within each country. In this sample dataset for example, I want to interpolate the value for Albania 2006, and extrapolate for Albania 2000-2004 and 2017. But I want to make sure that the value for Albania 2017 isn't interpolated using Albania 2016 and Algeria 2003. For Algeria 2000-2002, I want the values to be extrapolated using the data for Algeria 2003 and 2004. I have tried the following code:</p>
  
  <pre><code>data &lt;- group_by(data, country)
  data$broadband &lt;- na.approx(data$broadband, maxgap = Inf, rule = 2)
  data &lt;- as.data.frame(data)
  </code></pre>
  
  <p>and have tried different values for maxgap, but none seem to fix my problem. I assumed by using the group_by function it would work correctly but it doesn't. Does anyone know of any solutions?</p>
  
  <p>EDIT: The only way I have thought of to do what I need is to split the dataset into a separate dataset for each unique country, using the following code:</p>
  
  <pre><code>mylist &lt;- split(data, data$country)
  
  alb &lt;- mylist[1]
  alb &lt;- as_data_frame(alb)
  alg &lt;- mylist[2]
  alg &lt;- as_data_frame(alg)
  ang &lt;- mylist[3]
  ang &lt;- as_data_frame(ang)
  </code></pre>
  
  <p>and then use the na.approx function on the separate datasets one at a time.</p>
  
  <p>EDIT 2: </p>
  
  <p>I have tried the solution suggested by Markus below, and it doesn't seem to work. This is the result using your suggested coded for values for Angola:</p>
  
  <pre><code>Country Year    Broadband   Broadband_imp
  
  Algeria 2014    1599692 1599692
  Algeria 2015    2269348 2269348
  Algeria 2016    2858906 2858906
  Angola  2000    NA  2451556.286
  Angola  2001    NA  2044206.571
  Angola  2002    NA  1636856.857
  Angola  2003    NA  1229507.143
  Angola  2004    NA  822157.429
  Angola  2005    NA  414807.714
  Angola  2006    7458    7458
  Angola  2007    11700   11700
  </code></pre>
  
  <p>As you can see, the imputed values for Angola 2000-2005 seem to have been calculated using values from Algeria, as the imputed values are much higher than they should be given the Angola 2006 value of 7458.</p>
  
  <p>EDIT 3: This is the full code I have used - </p>
  
  <pre><code>data &lt;- read_excel("~/Documents/data.xlsx")
  
  &gt; dput(head(data))
  structure(list(continent = c("Europe", "Europe", "Europe", "Europe", 
  "Europe", "Europe"), country = c("Albania", "Albania", "Albania", 
  "Albania", "Albania", "Albania"), Year = c(2000, 2001, 2002, 
  2003, 2004, 2005), `Individuals Using Internet, %, WB` = c(0.114097347, 
  0.325798377, 0.390081273, 0.971900415, 2.420387798, 6.043890864
  ), `Secure Internet Servers, WB` = c(NA, 1, NA, 1, 2, 1), `Mobile Cellular 
  Subscriptions, WB` = c(29791, 
  392650, 851000, 1100000, 1259590, 1530244), `Fixed Broadband Subscriptions, 
  WB` = c(NA, 
  NA, NA, NA, NA, 272), `Trade, % GDP, WB` = c(55.9204287230026, 
  57.4303612453301, 63.9342407411882, 65.4406219482911, 66.3578254370479, 
  70.2953012017195), `Air transport, freight (million ton-km)` = c(0.003, 
  0.003, 0.144, 0.088, 0.099, 0.1), `Air Transport, registered carrier 
  departures worldwide, WB` = c(3885, 
  3974, 3762, 3800, 4104, 4309), `FDI, net, inflows, % GDP, WB` = 
  c(3.93717707227928, 
  5.10495722596557, 3.04391445388559, 3.09793068135411, 4.66563777108359, 
  3.21722676118428), `Number of Airports, WFB` = c(10, 11, 11, 
  11, 11, 11), `Currently under EU Arms Sanctions` = c(0, 0, 0, 
  0, 0, 0), `Currently under EU Economic Sanctions` = c(0, 0, 0, 
  0, 0, 0), `Currently under UN Arms Sanctions` = c(0, 0, 0, 0, 
  0, 0), `Currently under UN Economic Sanctions` = c(0, 0, 0, 0, 
  0, 0), `Currently under US Arms Embargo` = c(0, 0, 0, 0, 0, 0
  ), `Currently under US Economic Sanctions` = c(0, 0, 0, 0, 0, 
  0)), .Names = c("continent", "country", "Year", "Individuals Using Internet, 
  %, WB", 
  "Secure Internet Servers, WB", "Mobile Cellular Subscriptions, WB", 
  "Fixed Broadband Subscriptions, WB", "Trade, % GDP, WB", "Air transport, 
  freight (million ton-km)", 
  "Air Transport, registered carrier departures worldwide, WB", 
  "FDI, net, inflows, % GDP, WB", "Number of Airports, WFB", "Currently under EU 
   Arms Sanctions", 
  "Currently under EU Economic Sanctions", "Currently under UN Arms Sanctions", 
  "Currently under UN Economic Sanctions", "Currently under US Arms Embargo", 
  "Currently under US Economic Sanctions"), row.names = c(NA, -6L
  ), class = c("tbl_df", "tbl", "data.frame"))
  
   data_imputed &lt;- data %&gt;% 
  group_by(country) %&gt;% 
  mutate(broadband_imp = na.approx(broadband, maxgap=Inf, rule = 2))
  </code></pre>