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
29
votes
4 answers

Using cbind on an arbitrarily long list of objects

I would like to find a way to create a data.frame by using cbind() to join together many separate objects. For example, if A, B, C & D are all vectors of equal length, one can create data.frame ABCD with ABCD <- cbind(A,B,C,D) However, when the…
rtyro
  • 291
  • 1
  • 3
  • 3
25
votes
4 answers

Trouble converting long list of data.frames (~1 million) to single data.frame using do.call and ldply

I know there are many questions here in SO about ways to convert a list of data.frames to a single data.frame using do.call or ldply, but this questions is about understanding the inner workings of both methods and trying to figure out why I can't…
wahalulu
  • 1,357
  • 2
  • 16
  • 23
15
votes
5 answers

How to pass "nothing" as an argument to `[` for subsetting?

I was hoping to be able to construct a do.call formula for subsetting without having to identify the actual range of every dimension in the input array. The problem I'm running into is that I can't figure out how to mimic the direct function…
Carl Witthoft
  • 19,580
  • 8
  • 40
  • 67
13
votes
1 answer

R - merge a list of data frames into one data frame with missing values by row

I have a variation on the oh-so-common problem of how to merge things together in R. I have a set of .txt files in a particular folder, and I have written a function that: makes a list of the files I want, and then for each file reads the…
phosphorelated
  • 625
  • 2
  • 7
  • 21
12
votes
2 answers

merging multiple csv files in R using do.call

I am trying to import and merge a set of csv files using the following code, but it doesn't seem to be passing the by=c("X","Y") argument to the merge function. Any recommendations on how to fix this? Thank you csvfiles <- list.files(path = ".",…
ec0n0micus
  • 815
  • 1
  • 11
  • 19
12
votes
3 answers

How to combine do.call() plot() and expression()

I am getting an error when I try and combine using expression with do.call and plot. x <- 1:10 y <- x^1.5 I can get the plot I want by using only the plot function: plot(y~x,xlab=expression(paste("Concentration (",mu,"M)"))) However, I would…
dayne
  • 6,774
  • 4
  • 31
  • 49
11
votes
1 answer

adding ggtitle via do.call when argument is a language object

Consider a simple function, which adds a ggtitle to a grob f <- function(PLOT, TITLE) { PLOT + ggtitle(TITLE) } Calling the function directly works as expected. However, calling the function via do.call(f, ..) throws an error when TITLE is a…
Ricardo Saporta
  • 51,025
  • 13
  • 129
  • 166
11
votes
3 answers

Alternative to do.call for large datasets

I love do.call. I love being able to store function arguments in a list and then splatting them to a given function. For example, I often find myself using this pattern to fit a list of different predictive models, with some shared and some unique…
Zach
  • 27,553
  • 31
  • 130
  • 193
11
votes
3 answers

merge multiple data.frame by row in R

I would like to merge multiple data.frame in R using row.names, doing a full outer join. For this I was hoping to do the following: x = as.data.frame(t(data.frame(a=10, b=13, c=14))) y = as.data.frame(t(data.frame(a=1, b=2))) z =…
Alex
  • 17,745
  • 33
  • 112
  • 182
10
votes
2 answers

How do I call stargazer on a list of models?

I just ran a series of models in a nice, flexible way that enforced data-code separation. I had a nice list of formulas and models in my configuration section which I lapply'd over to get a list of model objects. Now I want to display them in…
Ari B. Friedman
  • 66,857
  • 33
  • 169
  • 226
10
votes
1 answer

How to avoid renaming of rows when using rbind inside do.call?

I am trying to bind some sub elements of the elements from the list The list OC is as follows > library(quantmod) > OC <- getOptionChain('AAPL', NULL) > str(OC) List of 9 $ Feb 2013:List of 3 ..$ calls :'data.frame': 35 obs. of 7 variables: …
CHP
  • 16,149
  • 4
  • 33
  • 56
8
votes
1 answer

Pass a named list of models to anova.merMod

I want to be able to pass a named list of models (merMod objects) to anova() and preserve the model names in the output. This is particularly useful in the context of using mclapply() to run a batch of slow models like glmers more efficiently in…
8
votes
5 answers

R: specifying a string as an argument of a function that calls another function

This is a question regarding coding in R. The example I provide is didactic. Suppose I have functions called 'func1' and 'func2', where each takes two arguments (let's say scalars). I want to specify another function 'applyfunction' that has three…
David Burk
  • 81
  • 2
8
votes
3 answers

R: Combine list of data frames into single data frame, add column with list index

The question is very similar to this one . It is for combining a list of data frames into a single longer data frame. However, I want to keep the information from which item of the list the data came from by adding an extra column with the index (id…
Jose R
  • 809
  • 9
  • 17
7
votes
3 answers

R: apply vs do.call

I just read the profile of @David Arenburg, and found a bunch of useful tips for how to develop good R-programming skills/habits, and one especially struck me. I have always thought that the apply functions in R was the cornerstone of working with…
Helen
  • 331
  • 9
  • 28
1
2 3
13 14