Use ddply within a function and include variable of interest as an argument

前端 未结 4 1099
南方客
南方客 2020-12-30 13:24

I am relatively new to R, and trying to use ddply & summarise from the plyr package. This post almost, but not quite, answers my question. I could use some additional ex

相关标签:
4条回答
  • 2020-12-30 13:26

    With the introduction of quosures in the devel version of dplyr (soon to be released 0.6.0), this becomes a bit more easier

    library(dplyr)
    descriptives_by_groupN <- function(dataset, group, x) {
    
       group <- enquo(group)
       x <- enquo(x)
    
      dataset %>%
             group_by(!!group) %>%
             summarise(Mean = mean(!!x),
                    SD = sd(!!x),
                    Min = min(!!x),
                    Max = max(!!x))
    }
    
    descriptives_by_groupN(mtcars, cyl, hp)
    # A tibble: 3 × 5
    #   cyl      Mean       SD   Min   Max
    #  <dbl>     <dbl>    <dbl> <dbl> <dbl>
    #1     4  82.63636 20.93453    52   113
    #2     6 122.28571 24.26049   105   175
    #3     8 209.21429 50.97689   150   335
    

    Here, the input arguments are converted to quosures with enquo, and inside the group_by/summarise, unquote the quosures (!! or UQ) to get it evaluated

    0 讨论(0)
  • 2020-12-30 13:39

    I just moved a couple things around in the example function you gave and showed how to get more than one column back out. Does this do what you want?

    myFunction2 <- function(x, y, col){
    z <- ddply(x, y, .fun = function(xx){
                             c(mean = mean(xx[,col],na.rm=TRUE),
                             max = max(xx[,col],na.rm=TRUE) ) })
    return(z)
    }
    
    myFunction2(mtcars, "cyl", "hp")
    
    0 讨论(0)
  • 2020-12-30 13:40

    (More of a comment than an answer. I had the same level of difficulty as you when using ddply(...,summarise, ...) inside a function.) This is a base solution that worked the way I expected:

    descriptives_by_group <- function(dataset, group, x)
      {aggregate(dataset[[x]], dataset[group], function(x)
          c(  mean = mean(x),
              sd   = sd(x),
              min  = min(x),
              max  = max(x)
             ) )
      }
    
    descriptives_by_group(mtcars, 'cyl', 'hp')
    
    0 讨论(0)
  • 2020-12-30 13:49

    Just use as.quoted function. Example below

    simple_ddply <- function(dataset_name, variable_name){
        data <- ddply(dataset_name,as.quoted(variable_name), *remaining input)**
    
    0 讨论(0)
提交回复
热议问题