简体   繁体   中英

How to avoid repeating code while using plyr

I want to produce same type of chart for some combination of data. Currently, I am using plyr to split the data and executing some code for each of the combination.

For example, let's say the dataframe has company, department, region, and revenue. Here's my pseudocode:

     d_ply(dataframe, .(company),  function(df) {
      d_ply(df, .(department),  function(df) {
        d_ply(df, .(region), function(df) {
           bar_chart(df$region, df$revenue)
        })
            bar_chart(df$department, df$revenue)
      })
            bar_chart(df$company, df$revenue)
    })

In my real example, I need to do multiple things, and the code is 10 or so lines. Is there a way to avoid repeating the code in each combination, other than creating a function and just passing the proper parameters? I was hoping that there is some magic plyr trick.

Dummy data:

d <- data.frame(company=letters[1:26],
                department=sample(letters[1:10],26,replace=TRUE),
                region=sample(letters[1:3],26,replace=TRUE),
                revenue=round(runif(26)*10000))

Update

I think an explanation of your code is necessary:

d_ply(dataframe, .(company),  function(df) { # by company
      d_ply(df, .(department),  function(df) { # by department
        d_ply(df, .(region), function(df) { # by region
           bar_chart(df$region, df$revenue)
           # this part is essentially equal to
           # d_ply(df, .(company,department,region), function(df), plot(df)) 
    })
  bar_chart(df$department, df$revenue)
  # this part is essentially equal 
  # d_ply(df,.(company,department), function(df), fun(df))
  })
 bar_chart(df$company, df$revenue)
 # this part is essentially equal to 
 # d_ply(df,.(company), function(df), fun(df))
})

I find your code to be highly unreadable. It could be replaced with:

some.fun <- function(df, ...) {
# ...
}

d_ply(d, .(company), function(df) some.fun(df, ...))
d_ply(d, .(company,department), function(df) some.fun(df, ...)) 
d_ply(d, .(company,department,region), function(df) some.fun(df, ...))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM