length fails in aggregate function

This is from some R code that worked at one time, and I can't figure what changed.

I want to use the aggregate() function to find the mean, sd, and length of the columns in the PlantGrowth data table.

library("datasets")
data("PlantGrowth")
aggregate(weight ~ group, data = PlantGrowth, mean)
aggregate(weight ~ group, data = PlantGrowth, sd)
aggregate(weight ~ group, data = PlantGrowth, length)

The first two work, producing the mean and standard deviation. The last one doesn't, giving me the error

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'FUN' of mode 'function' was not found

As far as I know, you can find the lengths (which is the number of observations, 10) using this method...or at least can't figure out why you can't.

It works on my system.

reprex with session info
aggregate(formula = weight ~ group,
          data = PlantGrowth,
          FUN = mean)
#>   group weight
#> 1  ctrl  5.032
#> 2  trt1  4.661
#> 3  trt2  5.526
aggregate(formula = weight ~ group,
          data = PlantGrowth,
          FUN = sd)
#>   group    weight
#> 1  ctrl 0.5830914
#> 2  trt1 0.7936757
#> 3  trt2 0.4425733
aggregate(formula = weight ~ group,
          data = PlantGrowth,
          FUN = length)
#>   group weight
#> 1  ctrl     10
#> 2  trt1     10
#> 3  trt2     10

Created on 2019-11-01 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_India.1252          
#>  ctype    English_India.1252          
#>  tz       Asia/Calcutta               
#>  date     2019-11-01                  
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.5   2019-10-02 [1] CRAN (R 3.6.1)
#>  callr         3.3.2   2019-09-22 [1] CRAN (R 3.6.1)
#>  cli           1.1.0   2019-03-19 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.1)
#>  devtools      2.2.1   2019-09-24 [1] CRAN (R 3.6.1)
#>  digest        0.6.22  2019-10-21 [1] CRAN (R 3.6.1)
#>  ellipsis      0.3.0   2019-09-20 [1] CRAN (R 3.6.1)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1   2019-05-06 [1] CRAN (R 3.6.0)
#>  glue          1.3.1   2019-03-12 [1] CRAN (R 3.6.0)
#>  highr         0.8     2019-03-20 [1] CRAN (R 3.6.0)
#>  htmltools     0.4.0   2019-10-04 [1] CRAN (R 3.6.1)
#>  knitr         1.25    2019-09-18 [1] CRAN (R 3.6.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.0)
#>  pkgbuild      1.0.6   2019-10-09 [1] CRAN (R 3.6.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.6.1)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.6.0)
#>  processx      3.4.1   2019-07-18 [1] CRAN (R 3.6.1)
#>  ps            1.3.0   2018-12-21 [1] CRAN (R 3.6.0)
#>  R6            2.4.0   2019-02-14 [1] CRAN (R 3.6.0)
#>  Rcpp          1.0.2   2019-07-25 [1] CRAN (R 3.6.1)
#>  remotes       2.1.0   2019-06-24 [1] CRAN (R 3.6.0)
#>  rlang         0.4.1   2019-10-24 [1] CRAN (R 3.6.1)
#>  rmarkdown     1.16    2019-10-01 [1] CRAN (R 3.6.1)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.1)
#>  stringi       1.4.3   2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat      2.2.1   2019-07-25 [1] CRAN (R 3.6.1)
#>  usethis       1.5.1   2019-07-04 [1] CRAN (R 3.6.1)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.10    2019-10-01 [1] CRAN (R 3.6.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] C:/Users/Anirban/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-3.6.1/library

Can you check whether you have used length as a variable name somewhere before? This problem may occur then.

It's better to not use function names as variable names. Anyway, you can use "length" instead of length. Putting quotes will help R understand that it's the function you want to use, though I don't completely understand how this works.

Glad to know it works, although still not on my system. I have not used length in any way in other places of my script, although I’ve done a day’s worth of computing in this R session.

Maybe try calling the length function directly with ::

aggregate(weight ~ group, data = PlantGrowth, base::length)
1 Like