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 9022029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:24:03+00:00 2026-06-16T05:24:03+00:00

This is an extension of Subtract previous year's from value from each grouped row

  • 0

This is an extension of Subtract previous year's from value from each grouped row in data frame. The option using plyr makes complete sense.

Now, I’m trying to add a couple more columns. I’ve also modified year so it’s an actual year with different starting points by id. Here is a link to tab-delimited df: https://dl.dropbox.com/u/9699533/df.txt

enter image description here

I am unable to get a pointer to the previous row when I’m in the current row. I’d like to pass this to the function part of plyr::transform. How do I write this, please? Thanks!

~~~~ Alternate dataframe input ~~~~~

> dput(df)
structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L), value = c(6L, 16L, 21L, 5L, 10L, 26L, 2L, 
12L, 26L, 9L, 16L, 26L, 2L, 15L, 29L), year = c(2007L, 2008L, 
2009L, 2011L, 2012L, 2013L, 2004L, 2004L, 2006L, 2010L, 2011L, 
2012L, 2014L, 2015L, 2016L), actual = c(6L, 10L, 5L, 5L, 5L, 
16L, 2L, 10L, 14L, 9L, 7L, 10L, 2L, 13L, 14L)), .Names = c("id", 
"value", "year", "actual"), class = "data.frame", row.names = c(NA, 
-15L))
  • 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-16T05:24:05+00:00Added an answer on June 16, 2026 at 5:24 am

    Don’t use transform.

    foo <- function(x){
       x$ratio <- c(NA,tail(x$actual,-1)/head(x$value,-1))
       x
     }
    > 
    > ddply(df,.(id),foo)
       id value year actual     ratio
    1   1     6 2007      6        NA
    2   1    16 2008     10 1.6666667
    3   1    21 2009      5 0.3125000
    4   2     5 2011      5        NA
    5   2    10 2012      5 1.0000000
    6   2    26 2013     16 1.6000000
    7   3     2 2004      2        NA
    8   3    12 2004     10 5.0000000
    9   3    26 2006     14 1.1666667
    10  4     9 2010      9        NA
    11  4    16 2011      7 0.7777778
    12  4    26 2012     10 0.6250000
    13  5     2 2014      2        NA
    14  5    15 2015     13 6.5000000
    15  5    29 2016     14 0.9333333
    

    If we can assume your data frame is sorted, and that we know how big each group (by id) is, we don’t even need to do the whole split-apply thing:

    df$ratio2 <- with(df,c(NA,tail(actual,-1) / head(value,-1)))
    df$ratio2[seq(1,nrow(df),by = 3)] <- NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using this extension to set specific events which handle data in the
I have downloaded this extension from http://www.blueflyingfish.com/orgchart/index.php?option=com_orgchart&view=entitytree&Itemid=53 for organizational chart . My Joomla version
this is an extension to my previous post today which can be found here:
I was using this extension method to transform very large xml files with an
I am using the TZ Portfolio extension for Joomla 2.5. In this extension, the
I have created my own extension. This extension can be accessed from different components
Using the emenu Yii extension for a menu. The underlying project for this extension
I am using this extension: Yii-Bootstrap , along with this less extension: less In
Consider I have this extension method: public static bool HasAnyFlagInCommon(this System.Enum type, Enum value)
I wrote this extension that basically changes the look of a website using CSS

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.