Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass extra argument to the function argument of do.call in R

Tags:

r

I'd like to pass argument (stringsAsFactors=FALSE) to rbind in do.call. But the following doesn't work:

data <- do.call(rbind,            strsplit(readLines("/home/jianfezhang/adoption.txt"), split="\t#\t"),            args=list(stringsAsFactors=FALSE)) 
like image 301
zjffdu Avatar asked Apr 12 '12 08:04

zjffdu


People also ask

Do call R with arguments?

call() function in R constructs and executes a function call from a name or a function as well as a list of arguments to be passed to it. In other words, the do. call() function allows us to call the R function using a list to hold the function's arguments instead of writing out the arguments.

How can we pass arguments in function calling?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.

Can functions take another function as an argument in R?

Yes. See the Examples section of ? optim and ? integrate for some R functions that accept other functions as arguments.


1 Answers

do.call(rbind.data.frame, c(list(iris), list(iris), stringsAsFactors=FALSE)) 

would have been my answer, if it wasn't for the fact that rbind does not know what to do with stringsAsFactors (but cbind.data.frame would).

The output of strsplit is presumably a list of vectors, in which case rbind creates a matrix. You can specify stringsAsFactors when converting this matrix to a data.frame,

data.frame(do.call(rbind, list(1:10, letters[1:10])), stringsAsFactors=FALSE) 
like image 56
baptiste Avatar answered Sep 23 '22 18:09

baptiste