[R] How does do.call() work??
Dear members of R forum, Say I have a list: L - list(1:3, 1:3, 1:3) that I want to turn into a matrix. I wonder why if I do: do.call(cbind, L) I get the matrix I want, but if I do cbind(L) I get something different from what I want. Why is that? How does do.call() actually work? I've read in do.call() help file this sentence: The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change. Thanks for help! Sergey -- I'm not young enough to know everything. /Oscar Wilde Experience is one thing you can't get for nothing. /Oscar Wilde When you are finished changing, you're finished. /Benjamin Franklin __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How does do.call() work??
On Jan 25, 2008 11:27 AM, Sergey Goriatchev [EMAIL PROTECTED] wrote: Dear members of R forum, Say I have a list: L - list(1:3, 1:3, 1:3) that I want to turn into a matrix. I wonder why if I do: do.call(cbind, L) I get the matrix I want, but if I do cbind(L) I get something different from what I want. Why is that? How does do.call() actually work? I've read in do.call() help file this sentence: The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change. Thanks for help! Sergey Try cbind(L[[1]],L[[2]],L[[3]]) ,which is equal to do.call(cbind,L). do.call takes a list of arguments, and feed each element of that list to the function. cbind takes two or more matrices, not a list of matrices as arguments. /Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How does do.call() work??
Sergey Goriatchev wrote: Dear members of R forum, Say I have a list: L - list(1:3, 1:3, 1:3) that I want to turn into a matrix. I wonder why if I do: do.call(cbind, L) I get the matrix I want, but if I do cbind(L) I get something different from what I want. Why is that? How does do.call() actually work? The second argument to do.call is args, a list of arguments to pass to the function (cbind in your case). The function doesn't know what to do when you pass it a list, it's expecting separate vectors/matrices. In your example, do.call(cbind, L) is equivalent to cbind(L[[1]], L[[2]], L[[3]]) I've read in do.call() help file this sentence: The behavior of some functions, such as substitute, will not be the same for functions evaluated using do.call as if they were evaluated from the interpreter. The precise semantics are currently undefined and subject to change. substitute() does strange things; cbind uses standard rules, so this isn't a problem for it. Duncan Murdoch Thanks for help! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.