Questions tagged [do.call]

do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

do.call is an R command that takes a list of arguments, executes a function call and then returns the result of the function call. Full documentation here

202 questions
4
votes
3 answers

Apply function to data.table using function's character name and arguments as character vector

I would like to call functions by their character name on a data.table. Each function has also a vector of arguments (so there is a long list of functions to apply to data.table). Arguments are data.table columns. My first thought was that do.call…
wasyl
  • 65
  • 4
4
votes
1 answer

why function rbind.data.frame behave different in do.call

I have a question about do.call, pretty strange 1. What I trying to do I am trying to bind many data frames into one single data frame, all the data frames are in a list t3, you may see the pic below: 2. Methods 2.1 the one works t4 <-…
huangli
  • 454
  • 9
  • 24
4
votes
1 answer

Pass object name into do.call() function

Is there a way to pass object name into do.call() function? For example: #First make a function that will return the name of object itself. PrintObjectName <- function(obj){ print(deparse(substitute(obj))) } data(iris) PrintObjectName(iris) [1]…
ChaoYang
  • 797
  • 2
  • 7
  • 14
4
votes
2 answers

How do I pass every element of a list to a function as unnamed arguments?

Let's say I have some models stored in a list: mods <- list() mods[[1]] <- lm(mpg ~ disp, data = mtcars) mods[[2]] <- lm(mpg ~ disp + factor(cyl), data = mtcars) mods[[3]] <- lm(mpg ~ disp * factor(cyl), data = mtcars) And I want to compare them…
Gregor Thomas
  • 104,719
  • 16
  • 140
  • 257
4
votes
2 answers

extracting segments of a data.table

I have a data.table and I need to extract equal length segments starting at various row locations. What is the easiest way to do this? For example: x <- data.table(a=sample(1:1000,100), b=sample(1:1000,100)) r <- c(1,2,10,20,44) idx <- lapply(r,…
Alex
  • 17,745
  • 33
  • 112
  • 182
3
votes
4 answers

R: Evaluate function for multiple and differently-changing arguments

I have a function with 4 arguments: my.function <- function(w,x,y,z){ w + x + y + z } I can give this function multiple values for z: > my.function(1,1,1,1:5) [1] 4 5 6 7 8 But what if I want to give the function lists of values for…
jayb
  • 491
  • 3
  • 14
3
votes
6 answers

Transform a nested list to dataframe

I have a list of items, each of them has two items, one is a list and the other one is a character expression We generate de lists My_list <- list() My_list$'product1' <- list() My_list$'product1'$'sales' <- c(1,2,3) My_list$'product1'$'model'…
lucas
  • 449
  • 1
  • 8
3
votes
3 answers

Non-standard evaluation in a user-defined function with lapply or with in R

I wrote a wrapper around ftable because I need to compute flat tables with frequency and percentage for many variables. As ftable method for class "formula" uses non-standard evaluation, the wrapper relies on do.call and match.call to allow the use…
Thomas
  • 181
  • 9
3
votes
2 answers

multiply two lists of irregular length

I have lists like a <- list(list(c(-2,1), 4:5, 2:3), list(c(0,2), c(-1,1))) b <- list(7:9, c(5,-1)) > a [[1]] [[1]][[1]] [1] -2 1 [[1]][[2]] [1] 4 5 [[1]][[3]] [1] 2 3 [[2]] [[2]][[1]] [1] 0 2 [[2]][[2]] [1] -1 1 > b [[1]] [1] 7 8…
Christoph Hanck
  • 353
  • 4
  • 14
3
votes
1 answer

pass result of one dplyr "do" function to another function

im trying to run regressions by group and then pass the regression model object to another function. library("lmtest") library("broom") library("tidyr") library("dplyr") library("purrr") fitted_models <- mtcars %>% group_by(gear) %>% …
hamiq
  • 375
  • 1
  • 2
  • 9
3
votes
5 answers

Duplicate the rows based on some criteria in SQL or R

I use R to generate a toy set data.frame(name = c("Tom", "Shane", "Daniel", "Akira", "Jack", "Zoe"), c1 = c(1,2,3,0,5,0), c2 = c(0, 3, 5, 0,4,0), c3 = c(0, 0,1,0,0,3), c4=c(0,0,0,1,0,0)) which is displayed below: I only care about the columns c1,…
Akira
  • 293
  • 4
  • 13
3
votes
2 answers

Convert Tibble to Parameter List

I am trying to convert a Tibble to a parameter list for a function call. The reason I am doing this is because I want to create a simple file specification Tibble for reading in multiple fixed width files with varying columns. This way I only need…
3
votes
1 answer

do.call() doesn't like base function "c" with a list

I have a larger section of code but I've narrowed down the problem to this - So I want to return a concatenated list. do.call(c,"X") Error in do.call(c, "X") : second argument must be a list So above it complains about the SECOND argument not…
Doug C
  • 33
  • 5
3
votes
2 answers

Can you use fix via do.call?

I have some code where it is more convenient to call fix via do.call, rather than directly. Any old data frame will work for this example: dfr <- data.frame(x = 1:5, y = letters[1:5]) The obvious first attempt is do.call("fix",…
Richie Cotton
  • 107,354
  • 40
  • 225
  • 343
3
votes
1 answer

R - How to handle the dot-dot-dot (ellipis/"...") with multiple subsequent functions - i.e. passing only some of the variables

I'm working on a non-linear optimization, with the constrOptim.nl from the alabama package. However, my problem is more related to passing arguments (and the dot-dot-dot (ellipis/"...") and maybe do.call)- so I give first a general example and later…
1 2
3
13 14