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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:26:34+00:00 2026-06-06T23:26:34+00:00

Sometimes I want to perform a function (eg difference calculation) on a dataset and

  • 0

Sometimes I want to perform a function (eg difference calculation) on a dataset and store the results directly in the data frame

df <- data.frame(a$C, diff(a$C))

But I cannot do that because the number of rows is different.

Is there some syntax that will allow me to to that, perhaps having NA when the function (diff()) gives no results?

  • 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-06T23:26:35+00:00Added an answer on June 6, 2026 at 11:26 pm

    There isn’t a general solution to this without making vast assumptions about the whole panoply of function one may wish to use.

    For the example you show, we can easily work out that the first value from diff() would be an NA if it returned it:

    set.seed(5)
    d <- rpois(10, 5)
    
    > d
     [1] 3 6 8 4 2 6 5 7 9 2
    > diff(d)
    [1]  3  2 -4 -2  4 -1  2  2 -7
    

    So if you are using diff() then you can always just do:

    > dd <- data.frame(d, Diff = c(NA, diff(d)))
    > dd
       d Diff
    1  3   NA
    2  6    3
    3  8    2
    4  4   -4
    5  2   -2
    6  6    4
    7  5   -1
    8  7    2
    9  9    2
    10 2   -7
    

    But now consider what you would do with any other function that you might wish to use that doesn’t always return NA in the correct place.

    For this example, we can use the zoo package which has an na.pad argument:

    require(zoo)
    d2 <- as.zoo(d)
    ddd <- data.frame(d, Diff = diff(d2, na.pad = TRUE))
    
    > ddd
       d Diff
    1  3   NA
    2  6    3
    3  8    2
    4  4   -4
    5  2   -2
    6  6    4
    7  5   -1
    8  7    2
    9  9    2
    10 2   -7
    

    If you are using a modelling function with a formula interface (e.g. lm()) and that function has an na.action argument, then you can set na.action = na.exclude in the function call and extractor functions such as fitted(), resid() etc will add back in to their output NA in the correct places so that the output is of the same length as the data passed to the modelling function.

    If you have other more specific cases you want to explore, please edit your Answer. In specific cases there will usually be a simple Answer to your Q. In the general case the Answer is no, it is not possible to do what you ask.

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

Sidebar

Related Questions

Sometimes I need to perform a rebuild of my project but I don't want
Sometimes I want to build Python or GCC from scratch just for fun, but
I know that Java have its own garbage collection, but sometimes I want to
Windows's Snipping tool can capture the screen, but sometimes I want to capture the
Sometimes I need to perform following command cp -rv demo demo_bkp However I want
If I'm not online, I sometimes want to package some changes to a commit,
When using implicit waits, as advised here , I still sometimes want to assert
Sometimes I want to have temporary comments fully left justified on a line (//)
Sometimes I want to return the value that is a property of an object
As I learn new features in Oracle, sometimes I want to test how a

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.