miller_2555
2009-Aug-26 16:20 UTC
[R] Applying do.call to a data.frame using function arguments
I'm trying to convert a data.frame to a series of strings (row-wise). There was a very good discussion awhile back (2002) entitled "[R] string concatenate across rows of a matrix??" where Tony Plate recommended the following two alternatives (x2 is an R object of type data frame -- a matrix also works for solution #1): 1) apply(format(x2), 1, paste, collapse=" "); 2) do.call("paste",x2) I applied each solution to a 1000x7 data frame, and found the do.call function takes <75% of the average time taken by the apply function on my particular system (repeated the comparison a few thousand times). I would like to use the do.call option for the x2 data frame for the increased performance, but cannot determine how I can also feed arguments to the paste function (specifically, I am trying to create comma-delimited values instead of space-delimited with each value wrapped in quotes). For instance: x2 <- as.data.frame(rbind(c(1,2,3),c(4,5,6))); => [1] " 1\",\"2\",\"3 " [2] " 4\",\"5\",\"6 " This result is obtained via the following command: apply(format(x2),1,paste,collapse='","',sep=''); => 1 2 "1\",\"2\",\"3" "4\",\"5\",\"6" However, I cannot obtain the same result using do.call. The best I have managed is the following code: do.call("paste",list(what=x2,collapse=',')); Unfortunately, the list function appears to mangle the data.frame w/r/t the do.call function. Is anyone aware of the appropriate call (or other optimized function that runs at least as fast as the do.call function)? -- View this message in context: http://www.nabble.com/Applying-do.call-to-a-data.frame-using-function-arguments-tp25151441p25151441.html Sent from the R help mailing list archive at Nabble.com.
miller_2555
2009-Aug-26 16:31 UTC
[R] Applying do.call to a data.frame using function arguments
miller_2555 wrote:> > I'm trying to convert a data.frame to a series of strings (row-wise). > There was a very good discussion awhile back (2002) entitled "[R] string > concatenate across rows of a matrix??" where Tony Plate recommended the > following two alternatives (x2 is an R object of type data frame -- a > matrix also works for solution #1): > 1) apply(format(x2), 1, paste, collapse=" "); > 2) do.call("paste",x2) >Nevermind. Stupid question. The solution is: do.call("paste",c(x2,sep='","')); Hope this helps somebody. -- View this message in context: http://www.nabble.com/Applying-do.call-to-a-data.frame-using-function-arguments-tp25151441p25151445.html Sent from the R help mailing list archive at Nabble.com.