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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:12:50+00:00 2026-05-31T06:12:50+00:00

I’m a bit confused. I routinely use transform like this ddply(data.frame, 1, transform, new.column

  • 0

I’m a bit confused. I routinely use transform like this

    ddply(data.frame, 1, transform, new.column = function(old.col.1,old.col.2,...))

This is also recommended by Hadley.

But recently I asked a question and Hadley stated this:

Don’t use transform. It’s a helper function suitable for interactive use, not for programming with.

So whats wrong with transform? I think im convinced now that this is stupid:

   transform(data.frame,col2=fun(col1)).

But is it not very useful in the ddply setting?

  • 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-31T06:12:51+00:00Added an answer on May 31, 2026 at 6:12 am

    There’s a difference between using transform within ddply and the function transform() as a standalone. It is far better (and quicker) to just do:

    Mydata$col3 <- fun(Mydata$col1, Mydata$col2)
    

    The function combination ddply/transform is especially useful if you have more than one column to change, eg

    Mynewdata <- ddply(Mydata,1,transform,col3=fun1(col1,col2), col4=fun2(col1,col2))
    

    And even then, you have the more flexible option of using within() that allows you to use calculated results to calculate the next row:

    Mynewdata <- within(Mydata,{
        col2 <- fun1(col1)
        col3 <- fun2(col1,col2)
    })
    

    The thing with transform() is that it is especially written to be used interactively. If you use it within a function, you might run into trouble. It is similar to subset() in that way: They’re convenience functions, but they’re neither fast nor very safe to use within more complex code.

    Opinions differ on ddply(). In some cases it works quick and gives very clean and readible code, in other cases I consider it serious overkill. ddply() often works faster and easier when you have to use non-vectorized functions, in which case the above options wouldn’t work. But for that, you also have the option to use mapply:

    Mynewdata <- within(Mydata, col3 <- mapply(myfun,col1,col2))
    

    mapply can in this case also be quite faster. To give you a basic example:

    Mydata <- data.frame(col1=rnorm(5),col2=rpois(5,3))
    myfun <- function(x,y){
        if(y == 0) mean(x) else
         mean(c(x,seq(1,y,by=1)))
    }
    
    code1 <- expression(Newdata <- ddply(Mydata,1,transform,col3=myfun(col1,col2)))
    code2 <- expression(Newdata2 <- within(Mydata, col3 <- mapply(myfun,col1,col2)))
    
    > benchmark(code1,code2)
       test replications elapsed relative 
    1 code1          100    0.50     12.5 
    2 code2          100    0.04      1.0 
    

    The main problem I have with ddply() is that the order of your observations is not guaranteed, as you see in the example output below:

    Mydata              Newdata2                    Newdata
            col1 col2         col1 col2      col3         col1 col2      col3
    1 0.07060223    4 | 0.07060223    4 2.0141204 | 0.05658259    2 1.0188609
    2 1.84645791    2 | 1.84645791    2 1.6154860 | 0.07060223    4 2.0141204
    3 0.05658259    2 | 0.05658259    2 1.0188609 | 0.84119845    1 0.9205992
    4 0.89998084    5 | 0.89998084    5 2.6499968 | 0.89998084    5 2.6499968
    5 0.84119845    1 | 0.84119845    1 0.9205992 | 1.84645791    2 1.6154860
    

    Both functions calculate the correct result, but mapply() does so faster in this case and with preserving the order of the observations in the dataframe.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.