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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:13:12+00:00 2026-05-14T07:13:12+00:00

I am trying to transform my data.frame by calculating the log-differences of each column

  • 0

I am trying to transform my data.frame by calculating the log-differences of each column
and controlling for the rows id. So basically I like to calculate the growth rates for each id’s variable.
So here is a random df with an id column, a time period colum p and three variable columns:

df <- data.frame (id = c("a","a","a","c","c","d","d","d","d","d"),
                  p = c(1,2,3,1,2,1,2,3,4,5),
                  var1 = rnorm(10, 5),
                  var2 = rnorm(10, 5),
                  var3 = rnorm(10, 5)
                  )
df
     id p     var1     var2     var3
1     a 1 5.375797 4.110324 5.773473
2     a 2 4.574700 6.541862 6.116153
3     a 3 3.029428 4.931924 5.631847
4     c 1 5.375855 4.181034 5.756510
5     c 2 5.067131 6.053009 6.746442
6     d 1 3.846438 4.515268 6.920389
7     d 2 4.910792 5.525340 4.625942
8     d 3 6.410238 5.138040 7.404533
9     d 4 4.637469 3.522542 3.661668
10    d 5 5.519138 4.599829 5.566892

Now I have written a function which does exactly what I want BUT I had to take a detour which is possibly unnecessary and can be removed. However, somehow I am not able to locate
the shortcut.
Here is the function and the output for the posted data frame:

fct.logDiff <- function (df) {
df.log <- dlply (df, "code", function(x) data.frame (p = x$p, log(x[, -c(1,2)])))
list.nalog <- llply (df.log, function(x) data.frame (p = x$p, rbind(NA, sapply(x[,-1], diff))))
ldply (list.nalog, data.frame)
}

 fct.logDiff(df)
     id p        var1        var2        var3
1     a 1          NA          NA          NA
2     a 2 -0.16136569  0.46472004  0.05765945
3     a 3 -0.41216720 -0.28249264 -0.08249587
4     c 1          NA          NA          NA
5     c 2 -0.05914281  0.36999681  0.15868378
6     d 1          NA          NA          NA
7     d 2  0.24428771  0.20188025 -0.40279188
8     d 3  0.26646102 -0.07267311  0.47041227
9     d 4 -0.32372771 -0.37748866 -0.70417351
10    d 5  0.17405309  0.26683625  0.41891802

The trouble is due to the added NA-rows. I don’t want to collapse the frame and reduce it, which would be automatically done by the diff() function. So I had 10 rows in my original frame and am keeping the same amount of rows after the transformation. In order to keep the same length I had to add some NAs. I have taken a detour by transforming the data.frame into a list, add the NAs to each id’s first line, and afterwards transform the list back into a data.frame. That looks tedious.

Any ideas to avoid the data.frame-list-data.frame class transformation and optimize the function?

  • 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-14T07:13:12+00:00Added an answer on May 14, 2026 at 7:13 am

    How about this?

    nadiff <- function(x, ...) c(NA, diff(x, ...))
    ddply(df, "code", colwise(nadiff, c("var1", "var2", "var3")))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an as.data.frame.by method which basically melts the N-dimensional by object
I'm trying to use XSL-stylesheets in order to transform some generated XML-data to HTML-output.
I'm trying to transform some XML-data to HTML with XSLT for my bachelor thesis.
I'm trying to canonicalize the representation of some XML data by sorting each element's
I need to calculate and add to a data frame multiple new columns based
I'm trying to transform each element of a numpy array into an array itself
I'm trying to transform some data selected out of a repository using Linq. My
I'm trying to take a dataset that looks like this: And transform the records
I'm trying to transfer some data from Access to SQL Server 2000 (there are
I'm trying to read the POST data from a request with Transfer-Encoding: chunked while

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.