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

231 questions
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
  • 533
  • 12
  • 37
7
votes
1 answer

do.call rbind of data.table depends on location of NA

Consider this do.call(rbind, list(data.table(x=1, b='x'),data.table(x=1, b=NA))) returns x b 1: 1 x 2: 1 NA but do.call(rbind, list(data.table(x=1, b=NA),data.table(x=1, b='x'))) returns x b 1: 1 NA 2: 1 NA How can i force the first…
Sapsi
  • 711
  • 5
  • 16
7
votes
4 answers

interweave two data.frames in R

I would like to interweave two data.frame in R. For example: a = data.frame(x=1:5, y=5:1) b = data.frame(x=2:6, y=4:0) I would like the result to look like: > x y 1 5 2 4 2 4 3 3 3 3 ... obtained by cbinding x[1] with y[1], x[2] with…
Alex
  • 19,533
  • 37
  • 126
  • 195
7
votes
3 answers

lapply and do.call running very slowly?

I have a data frame that is some 35,000 rows, by 7 columns. it looks like this: head(nuc) chr feature start end gene_id pctAT pctGC length 1 1 CDS 67000042 67000051 NM_032291 0.600000 0.400000 10 2 1 CDS 67091530…
Davy Kavanagh
  • 4,809
  • 9
  • 35
  • 50
6
votes
3 answers

how to combine vectors with different length within a list in R?

I have a problem when combining the following vectors included in the list: x <- list(as.numeric(c(1,4)),as.numeric(c(3,19,11))) names (x[[1]]) <- c("species.A","species.C") names (x[[2]]) <- c("species.A","species.B","species.C") which gives the…
Julien
  • 509
  • 1
  • 5
  • 8
6
votes
3 answers

do.call doesn't work with "+" as "what" and a list of 3+ elements

I can use do.call to sum two vectors elementwise: do.call(what="+", args =list(c(0,0,1), c(1,2,3)) >[1] 1 2 4 However, if I'd like to call the same operator with a list of three vectors, it fails: do.call(what = "+", args = list(c(0,0,1), c(1,2,3),…
Emile Zäkiev
  • 150
  • 1
  • 12
6
votes
1 answer

R: Error in pi[[j]] : subscript out of bounds -- rbind on a list of dataframes

I am trying to rbind a large list of data frames (outputDfList), which is generated by lapply a complicated function to a large table. You can recreate outputDfList by: df1=data.frame("randomseq_chr15q22.1_translocationOrInsertion", "chr15",…
Helene
  • 953
  • 3
  • 12
  • 22
6
votes
1 answer

Why do rbind() and do.call(rbind, ) return different results?

I want to convert a list to a data frame, with the following code: ls<-list(a=c(1:4),b=c(3:6)) do.call("rbind",ls) The result obtained by adding do.call is as shown below. It returns a data.frame object as desired. do.call("rbind",ls) [,1] [,2]…
eclo.qh
  • 155
  • 1
  • 2
  • 8
6
votes
3 answers

do.call specify environment inside function

I'm using the following construct in a package, ## two functions in the global environment funa <- function(x) x^2 funb <- function(x) x^3 ## called within a function, fine fun_wrap <- function(){ lapply(c('funa', 'funb'), do.call,…
baptiste
  • 75,767
  • 19
  • 198
  • 294
5
votes
2 answers

Using na.rm=T in pmin with do.call

I want to extract the minimum value of each element of several matrix that are stored inside a list. I'm using pmin: do.call(pmin, mylist) The problem is that some elements of those matrix are NAs, and pmin yields a NA where I want it to yield the…
MilloMarinE
  • 132
  • 8
5
votes
2 answers

Merge multiple .csv files into one

I am aware this question has been asked multiple times, but despite of trying to apply the aforementioned solutions i was not able to solve my little problem: I have saved all my .csv that i am aiming to merge into one folder: > file_list <-…
Nneka
  • 1,764
  • 2
  • 15
  • 39
5
votes
3 answers

Behavior of do.call() in the presence of arguments without defaults

This question is a follow-up to a previous answer which raised a puzzle. Reproducible example from the previous answer: Models <- list( lm(runif(10)~rnorm(10)),lm(runif(10)~rnorm(10)),lm(runif(10)~rnorm(10)) ) lm1 <-…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
4
votes
1 answer

How to pass call object into do.call

Giving a classification function ranger, I want to get all the parameters with their default values from its definition using formals. Then, I want to change some default values and use them as parameter with do.call library(ranger) # Getting all…
Lev
  • 693
  • 1
  • 8
  • 24
4
votes
1 answer

do.call with options in r to arrange ggplot list

I have my dummy data as: x=1:7 y=1:7 df = data.frame(x=x,y=y) bp <- vector("list", length = 4) for (i in 1:4) { bp[[i]] <- ggplot(df,aes(x,y))+geom_point() } I have my ggplot objects in a list called bp with which I can generate a four-plot-grid…
MAPK
  • 5,635
  • 4
  • 37
  • 88
4
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…
1
2
3
15 16