Using fun1 and fun2 from the question we can define both which sequentially runs them. We also test it.

both <- function(...) {
  fun1(...)
  fun2(...)
}

# test
both(1, 2)
## [1] 2
## [1] 3

Or if you wanted to return their outputs:

both2 <- function(...) list(fun1(...), fun2(...))

Alternately, we can define a factory function which given two functions having the same arguments creates a function which runs them in sequence.

Combine <- function(FUN1, FUN2) {
  function(...) { FUN1(...); FUN2(...) }
}

# test 
mewfun <- Combine(fun1, fun2)
newfun(1, 2)
## [1] 2
## [1] 3

or if you wnated to return their outputs

Combine2 <- function(FUN1, FUN2) {
  function(...) list(FUN1(...), FUN2(...))
}
Answer from G. Grothendieck on Stack Overflow
Top answer
1 of 3
1

For future reference you perhaps could have supplied some example data in order to showcase the problem. As i read your question, you're looking for a way to compose two functions (i.e. h(x) = g(f(x)). This can be done as follows:

myDat <- function(filename) {
  dat <- readModels(filename, what = "parameters")$parameters  
  dat <- filter(as.data.frame(dat$unstandardized), 
                grepl("BY|Intercepts", paramHeader))
  return(dat)
}

loading <- function(data) {
  data %>% 
    filter(grepl("BY", paramHeader)) %>% 
    select(c(param, est)) %>% 
    separate(param, c("loading","time")) %>%
    mutate(time = paste0('time',time)) %>% 
    spread(loading, est)
}

combined <- purrr::compose(loading, myDat)

Created on 2020-01-27 by the reprex package (v0.3.0)

2 of 3
0

We can have everything in single pipe. After extracting the 'parameters', pull the 'unstandardized', convert to data.frame, then filter the 'paramHeader` where the values are 'BY' or 'Intercepts', and combine with the rest of the pipe from the second function

library(dplyr)
library(stringr)
library(tidyr)
loadingNew <- function(filename) {
   readModels(filename, what = "parameters")$parameters  %>%
    pull('unstandardized') %>%
    as.data.frame %>%        
    filter(str_detect(paramHeader, "BY|Intercepts")) %>%   # not clear about two filter
    filter(grepl("BY", paramHeader)) %>% 
    select(param, est) %>% 
    separate(param, c("loading","time")) %>%
    mutate(time = str_c('time',time)) %>% 
    spread(loading, est)
    # or use pivot_wider
    #pivot_wider(names_from = loading, values_from = est)


}

loadingNew("cfa.out")
🌐
Rdrr.io
rdrr.io › cran › plyr › man › each.html
each: Aggregate multiple functions into a single function. in plyr: Tools for Splitting, Applying and Combining Data
October 2, 2023 - Combine multiple functions into a single function returning a named vector of outputs. Note: you cannot supply additional parameters for the summary functions ... # Call min() and max() on the vector 1:10 each(min, max)(1:10) # This syntax looks a little different. It is shorthand for the # the following: f<- each(min, max) f(1:10) # Three equivalent ways to call min() and max() on the vector 1:10 each("min", "max")(1:10) each(c("min", "max"))(1:10) each(c(min, max))(1:10) # Call length(), min() and max() on a random normal vector each(length, mean, var)(rnorm(100))
🌐
GeeksforGeeks
geeksforgeeks.org › r language › concatenating-objects-in-r-programming-combine-function
Concatenating Objects in R Programming - combine() Function - GeeksforGeeks
July 15, 2025 - Syntax: combine(x, y, ...) Parameters: x, y, .... are vectors to combine Example 1: In this example, we'll see the same output of both these functions on same operation. ... # Package required install.packages("dplyr") # Load the library ...
🌐
RDocumentation
rdocumentation.org › packages › gdata › versions › 2.18.0 › topics › combine
combine function - RDocumentation
The source column is created as a factor with levels corresponding to the name of the object from which the each row was obtained. When the names argument is ommitted, the name of each object is obtained from the specified argument name in the call (if present) or from the name of the object. See below for examples. ... # NOT RUN { a <- matrix(rnorm(12),ncol=4,nrow=3) b <- 1:4 combine(a,b) combine(x=a,b) combine(x=a,y=b) combine(a,b,names=c("one","two")) c <- 1:6 combine(b,c) # }
🌐
GeeksforGeeks
geeksforgeeks.org › concatenating-objects-in-r-programming-combine-function
Concatenating Objects in R Programming – combine() Function | GeeksforGeeks
August 31, 2020 - union() function in R Language is used to combine the data of two objects. This function takes two objects like Vectors, dataframes, etc. as arguments and results in a third object with the combination of the data of both the objects.
🌐
Statistics Globe
statisticsglobe.com › home › learn r programming (tutorial & examples) | free introduction › combine r function of dplyr package (2 examples)
combine R Function of dplyr Package (2 Examples) | c() vs. combine()
March 17, 2022 - The difference between c() and combine() is that the combine function follows the general rules of the tidyverse environment. These rules are similar to the general rules of Base R. However, there are some differences. One of these differences occurs when factor vectors should be concatenated. Let’s have a look at this in practice!
Find elsewhere
🌐
HBC Training
hbctraining.github.io › Intro-to-R › lessons › introR-nested-functions.html
Nested Functions | Introduction to R - ARCHIVED
May 9, 2017 - Instead of performing all three steps, we would like to create a nested function. We could first replace the sex_fr variable with the assignment in Step2: metadata_new <- cbind(metadata, gender=factor(sex)) Or we can go a step further and combine all steps, making your code slightly more difficult to read (we don’t recommend doing this):
🌐
Stack Overflow
stackoverflow.com › questions › 59073431 › how-to-combine-two-functions-in-r-statistics
How to combine two functions in R statistics? - Stack Overflow
id' just like to check, since you say the function "does not work", is it possible that the typo in the function name is the problem? That is, that you're perhaps defining the function dealAndSuffle (with no 'h'), but calling the function dealAndShuffle (with an 'h') ... sample() function randomly selects values and therefore even if the same function repeated twice you will get different results. To ensure your combined function gives you the same output, use set.seed() function when checking the results:
🌐
Bookdown
bookdown.org › yundai09 › rbootcamp › split-apply-combine.html
9 Split, Apply, Combine | R Bootcamp
Next, we calculate the number of female and male applicants in the admitted group. Our approach here is to get the numbers for both admitted and rejected applicants and extract the admitted applicants from the result. We need to find each combination by Admit and Gender to apply the function sum().
🌐
Stack Overflow
stackoverflow.com › questions › 46984797 › combine-multiple-different-functions-into-one-to-run-all-at-once-in-r
combine multiple different functions into one to run all at once in R - Stack Overflow
October 27, 2017 - Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... I have this code that creates a random sequence. `$seq.mat <- matrix(NA, nrow=20, ncol=2) colnames(seq.mat) <- c("SmapleID","Sequence") set.seed(123) for (i in 1:nrow(seq.mat)) { seq.mat[i,1] <- paste0("Sample_", i) seq.mat[i,2] <- paste(sample(c("C","G","A","T"), size=1000, replace = TRUE), collapse="") } seq.mat <- cbind(seq.mat, RNASeq=NA, Seq.Count=NA)` How can I use these 3? functions together in I guess a "multi-function" to run all at once?
🌐
GeeksforGeeks
geeksforgeeks.org › merge-function-in-r
Merge Function In R - GeeksforGeeks
April 17, 2024 - The merge() function in R provides a flexible and efficient way to combine data frames based on common columns or keys.
🌐
W3Schools
w3schools.com › r › r_variables_concat.asp
R Join / Concatenate Elements
Variables Concatenate Elements ... Loop R For Loop ... You can also concatenate, or join, two or more elements, by using the paste() function. To combine both text and a variable, R uses comma (,):...
🌐
RDocumentation
rdocumentation.org › packages › gdata › versions › 3.0.0 › topics › combine
Combine R Objects With a Column Labeling the Source
The source column is created as a factor with levels corresponding to the name of the object from which the each row was obtained. When the names argument is ommitted, the name of each object is obtained from the specified argument name in the call (if present) or from the name of the object. See below for examples. ... a <- matrix(rnorm(12),ncol=4,nrow=3) b <- 1:4 combine(a,b) combine(x=a,b) combine(x=a,y=b) combine(a,b,names=c("one","two")) c <- 1:6 combine(b,c)