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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:31:17+00:00 2026-05-28T05:31:17+00:00

sorry, for the cryptic title i didn’t find any better summary for my problem.

  • 0

sorry, for the cryptic title i didn’t find any better summary for my problem. So here’s my problem: i have a dataframe and want to make diff() over groups which works fine:

 df <- data.frame (name = rep(c("a", "b", "c"), 4),
              index = rep(c("c1", "c2"), each=6),
              year = rep(c(2008:2010),4),
              value = rep(1:3, each=4))

head(df)

  name index year value

1    a    c1 2008     1
2    b    c1 2009     1
3    c    c1 2010     1

ddply(df, .(name, year), summarize,  value=diff(value))

However, I would like to include the index in my result wich i tried to do with:

ddply(df, .(name, year), summarize,  value=diff(value), index=index)

Yet this yields the error message:

length(rows) == 1 is not TRUE

Which is I guess because the index has more rows because it is not processed by diff. Is there a quick solution to my problem?

Thank you very much!

EDIT

I try to clarify my question what I want to add to the result:

Suppose the variable index above. This is a factor that ought to explain something. Yet, I cannot take diff() of it that would not make sense so I just want to pass this one without changing anything. I tried drop==FALSE wich did yield the same error messsage.

Sorr for all this confusion! Here’s a very simple example:

name year  index  value
 a   2008    c1    10
 a   2009    c2    30
 a   2010    c1    40

after taking diff’s acroos group ‘a’ this looks like:

name year index d.value 
 a   2009  c2     +20  #c2 stayed the same just the first row got intentionally dropped.
 a   2010  c1     +10

consider the unfortunate name index as something like an attribute: it can change during the years but would not make sense to take a diff()

I really really hope this gives you a clue what I want – if not I’ll delete the question because I found an unelegant workaround 😉 and sorry for all the inconvenience!

  • 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-05-28T05:31:17+00:00Added an answer on May 28, 2026 at 5:31 am

    I’m not entirely sure what you want, it sounded like you want to get diffs, keeping the index variable and dropping the first row of each grouping. Does this get you what you want?

    doSummary = function(df) {
      values = diff(df$value)
      indexes = df$index[2:length(df)]
      data.frame(d.value=values, index=indexes)
    }
    ddply(df, .(name, year), doSummary)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry for the cryptic question title. I'm having a strange problem and i have
I am sorry for the cryptic title but I didn't know how to adequately
Sorry for the complicated title. Screenshot here: Basically: Do a Find in Files search
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry I could not find this. $('div#loader').slideDown(fast).delay(1.5); Does not word as expected, I want
(sorry about the long title) I have a custom object Person, which in turn
Sorry for the cryptic title.. Could you help me out, on how do a
Sorry I have a problem with with the fancy left apostrophe showing up in
Sorry the title is not very descriptive but it is a tricky problem to

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.