Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created July 8, 2025 06:29
Show Gist options
  • Select an option

  • Save njtierney/670a4fe2a0cac357630f863ae89dd397 to your computer and use it in GitHub Desktop.

Select an option

Save njtierney/670a4fe2a0cac357630f863ae89dd397 to your computer and use it in GitHub Desktop.
add <- function(x) Reduce(`+`, x)
list(1,2,3)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
sum(list(1,2,3))
#> Error in sum(list(1, 2, 3)): invalid 'type' (list) of argument
add(list(1, 2, 3))
#> [1] 6

mat1 <- matrix(runif(9), 3, 3)
mat2 <- matrix(runif(9), 3, 3)
mat3 <- matrix(runif(9), 3, 3)

mat1 + mat2 + mat2
#>           [,1]     [,2]      [,3]
#> [1,] 2.1002139 1.642836 2.2730969
#> [2,] 0.3521676 1.249252 1.4587838
#> [3,] 2.1345248 1.118134 0.3539423

mat_list <- list(mat1, mat2, mat3)

add(mat_list)
#>           [,1]     [,2]      [,3]
#> [1,] 2.0388184 1.295040 1.9468848
#> [2,] 0.2932382 1.037669 1.6180595
#> [3,] 1.3348547 1.490285 0.6191251

Created on 2025-07-08 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.1 (2025-06-13)
#>  os       macOS Sonoma 14.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Hobart
#>  date     2025-07-08
#>  pandoc   3.6.3 @ /Applications/Positron.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>  quarto   1.7.31 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.4   2025-06-18 [1] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] /Users/nick_1/Library/R/arm64/4.5/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment