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

  • Home
  • SEARCH
  • 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 8207563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:02:28+00:00 2026-06-07T09:02:28+00:00

I often find myself needing to apply a small number of rule-based transformations to

  • 0

I often find myself needing to apply a small number of rule-based transformations to dataframes based on certain conditions, typically a fixed number of fields having certain values. The transformations can modify any number of columns, usually one to three. The number of rows involved in these transformations is small compared to the total number of rows in the data frames. Currently I am using ddply but the performance is lacking because ddply modifies all rows.

I am looking for a way to solve this problem in an elegant, generic manner taking advantage of the fact that only a small number of rows needs to be changed. Below is a simplified example of the types of transformations I’m dealing with.

df <- data.frame(Product=gl(4,10,labels=c("A","B", "C", "D")), 
                 Year=sort(rep(2002:2011,4)), 
                 Quarter=rep(c("Q1","Q2", "Q3", "Q4"), 10), 
                 Sales=1:40)           
> head(df)
  Product Year Quarter Sales
1       A 2002      Q1     1
2       A 2002      Q2     2
3       A 2002      Q3     3
4       A 2002      Q4     4
5       A 2003      Q1     5
6       A 2003      Q2     6
> 
transformations <- function(df) {
    if (df$Year == 2002 && df$Product == 'A') {
        df$Sales <- df$Sales + 3
    } else if (df$Year == 2009 && df$Product == 'C') {
        df$Sales <- df$Sales - 10
        df$Product <- 'E'
    }
    df
}

library(plyr)
df <- ddply(df, .(Product, Year), transformations)

> head(df)
  Product Year Quarter Sales
1       A 2002      Q1     4
2       A 2002      Q2     5
3       A 2002      Q3     6
4       A 2002      Q4     7
5       A 2003      Q1     5
6       A 2003      Q2     6

Insted of hard-coded conditionals I’m using a pairlist of conditional and transformation functions, e.g., the code below, but that’s not a meaningful improvement.

transformation_rules <- list(
  list(
    condition = function(df) df$Year == 2002 && df$Product == 'A',
    transformation = function(df) {
      df$Sales <- df$Sales + 3
      df
    }
  )
)

What are some better ways to approach this problem?

  • 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-07T09:02:29+00:00Added an answer on June 7, 2026 at 9:02 am

    I don’t think you need to use plyr for this problem at all. I think you can simply use ifelse() and take advantage of the fact that R is vectorized and get the same result.

    Since your function directly modifies the Sales column, I made a copy of it like so before running plyr: df2 <- df. I also had my example make a new column Sales2 instead of overwriting the Sales column.

    And then rewrote your function like this:

    df2$Sales2 <- with(df2, ifelse(Year == 2002 & Product == "A", Sales + 3,
                            ifelse(Year == 2009 & Product == "C", Sales - 10, Sales)))
    

    And testing whether or not the output is equal:

    > all.equal(df$Sales, df2$Sales2)
    [1] TRUE
    

    And comparing the system timing between the two shows that the vectorized version avoiding ddply is much faster:

    > system.time(df <- ddply(df, .(Product, Year), transformations))
       user  system elapsed 
      0.012   0.000   0.012 
    > system.time(df2$Sales2 <- with(df2, ifelse(Year == 2002 & Product == "A", Sales + 3,
    +                         ifelse(Year == 2009 & Product == "C", Sales - 10, Sales))))
       user  system elapsed 
          0       0       0 
    

    So, unless I’m missing something – you can avoid plyr all together here and get some nice speed improvements. If ifelse() proves too slow, you could write up some boolean functions to go even faster, though I doubt that will be necessary.

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

Sidebar

Related Questions

I often find myself needing reference to an object that is several objects away,
I find myself often needing to use int.TryParse() to test if a value is
I often find myself with a file that has one number per line. I
I often find myself needing to compose a list of items from attributes of
I often find myself needing a quick ( in terms of code ), lightweight
I often find myself needing to write functions to load/save from/to ASCII (or similar)
While programming I often find myself needing to calculate something like: x = (y
I find myself often needing to use Integer.TryParse to test if a value is
I often find myself needing to write code with the following logical pattern: $foo
I find myself often needing to bind an event handler, as well as immediately

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.