Jonathan Greenberg
2010-Jun-15 18:27 UTC
[R] Fastest way to merge matrix columns into a comma delimited string?
Folks: Say I have a matrix: test=matrix(c(1,2,3),nrow=10,ncol=3) I would like to have an output character vector where each line is row's values delimited by commas, e.g.: "1,2,3" "2,3,1" "3,1,2" ... "1,2,3" What is the fastest way of doing this? I can paste() row-by-row but this seems an inefficient approach to doing this. Thanks! I'm not asking how to write a CSV file, mind you, I want the array as an R data.frame or vector. --j
Sarah Goslee
2010-Jun-15 19:13 UTC
[R] Fastest way to merge matrix columns into a comma delimited string?
> apply(test, 1, paste, collapse=",")[1] "1,2,3" "2,3,1" "3,1,2" "1,2,3" "2,3,1" "3,1,2" "1,2,3" "2,3,1" "3,1,2" [10] "1,2,3" On Tue, Jun 15, 2010 at 2:27 PM, Jonathan Greenberg <greenberg at ucdavis.edu> wrote:> Folks: > > Say I have a matrix: > > test=matrix(c(1,2,3),nrow=10,ncol=3) > > I would like to have an output character vector where each line is > row's values delimited by commas, e.g.: > > "1,2,3" > "2,3,1" > "3,1,2" > ... > "1,2,3" >-- Sarah Goslee http://www.functionaldiversity.org
Peter Langfelder
2010-Jun-15 19:15 UTC
[R] Fastest way to merge matrix columns into a comma delimited string?
apply(test, 1, paste, collapse = ",") On Tue, Jun 15, 2010 at 11:27 AM, Jonathan Greenberg <greenberg at ucdavis.edu> wrote:> Folks: > > Say I have a matrix: > > test=matrix(c(1,2,3),nrow=10,ncol=3) > > I would like to have an output character vector where each line is > row's values delimited by commas, e.g.: > > "1,2,3" > "2,3,1" > "3,1,2" > ... > "1,2,3" > > What is the fastest way of doing this? ?I can paste() row-by-row but > this seems an inefficient approach to doing this. ?Thanks! ?I'm not > asking how to write a CSV file, mind you, I want the array as an R > data.frame or vector. > > --j > > ______________________________________________ > R-help at 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. >