Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:35:26+00:00 2026-06-01T00:35:26+00:00

I am trying to use plyr and approx to interpolate values for y for

  • 0

I am trying to use plyr and approx to interpolate values for y for each year between the observed values.

Instead of just the 3 observations for each country,

I would like to have 11 observations – one for each year from 1985 and 1995.

Here is a sample data set

country <- c("country a", "country a", "country a",
   "country b", "country b", "country b",
   "country c", "country c", "country c")
year <- c(1985, 1990, 1995,
      1985, 1990, 1995,
      1985, 1990, 1995)
y <- c(10, 12, 16,
   NA, 23, 20,
   12, 16, NA)

data <- data.frame(cbind(country,year,y))

The data set looks like this:
  country   year    y
1 country a 1985   10
2 country a 1990   12
3 country a 1995   16
4 country b 1985 <NA>
5 country b 1990   23
6 country b 1995   20
7 country c 1985   12
8 country c 1990   16
9 country c 1995 <NA>

I can get approx to work for a subset of the data with just one country

a <- subset(data, data$country == "country a")

interpolate y value for every year from 1985 to 1995

attach(a)
a.int <- approx(year,y, xout = 1985:1995, method = "linear")

But how do I use plyr to interpolate data for each country?

I’ve tried using dlply, but the output values are NA for each year

attach(data)
int <- dlply(data, .(country), function(i) approx(i$year, i$y, xout = 1985:1995, 
method = "linear")$y )

How can I use plyr and approx together to interpolate values of y?

Also, once I get the correct aprrox output (which will be list) how do I reshape the data so that it is in the original long format? Ideally, the data would have 11 rows each country and one column with y values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:35:28+00:00Added an answer on June 1, 2026 at 12:35 am

    I would use ddply rather than dlply for this.

    country <- c("country a", "country a", "country a",
       "country b", "country b", "country b",
       "country c", "country c", "country c")
    year <- c(1985, 1990, 1995,
          1985, 1990, 1995,
          1985, 1990, 1995)
    y <- c(10, 12, 16,
       NA, 23, 20,
       12, 16, NA)
    
    data <- data.frame(cbind(country,year,y)) 
    
    my.func<- function(i) {
      estimate <- approx(i$year,
                         i$y,
                         xout = 1985:1995,
                         method = "linear")
      return(data.frame(year=estimate$x, y=estimate$y, country=unique(i$country)))
    }
    
    > ddply(data, .(country),  my.func)
       year    y   country
    1  1985 10.0 country a
    2  1986 10.4 country a
    3  1987 10.8 country a
    4  1988 11.2 country a
    5  1989 11.6 country a
    6  1990 12.0 country a
    7  1991 12.8 country a
    8  1992 13.6 country a
    9  1993 14.4 country a
    10 1994 15.2 country a
    11 1995 16.0 country a
    12 1985   NA country b
    13 1986   NA country b
    14 1987   NA country b
    15 1988   NA country b
    16 1989   NA country b
    17 1990 23.0 country b
    18 1991 22.4 country b
    19 1992 21.8 country b
    20 1993 21.2 country b
    21 1994 20.6 country b
    22 1995 20.0 country b
    23 1985 12.0 country c
    24 1986 12.8 country c
    25 1987 13.6 country c
    26 1988 14.4 country c
    27 1989 15.2 country c
    28 1990 16.0 country c
    29 1991   NA country c
    30 1992   NA country c
    31 1993   NA country c
    32 1994   NA country c
    33 1995   NA country c
    
    sessionInfo()
    R version 2.14.2 (2012-02-29)
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    locale:
     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=C                 LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  grid      methods   base     
    
    other attached packages:
    [1] ggplot2_0.8.9  proto_0.3-9.2  reshape_0.8.4  reshape2_1.2.1 plyr_1.7.1    
    
    loaded via a namespace (and not attached):
    [1] stringr_0.6
    

    However, approx by default returns NA for values outside the min or max X supplied. see ?approx for the different methods for changing this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to use ddply in the plyr library in R, with
I'm trying to use the daply function in the plyr package but I cannot
I am trying use the django ORM to get a list by year of
Hi I'm trying use a mixin to define some method using define_method. I would
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.