I have a vector, (not a list)> repeated.measures.FACTOR.names[1] "Insp1" "Insp2" "Insp3" "Insp4" "Insp5" "Insp6" "Insp7" "Insp8" "Insp9" and would like to convert this into a single string "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" I can do that with a loop, but isn't there a more elegant way?> result <- repeated.measures.FACTOR.names[[1]] > for(i in 2:length(repeated.measures.FACTOR.names)) {result <- paste(result, repeated.measures.FACTOR.names[[i]], sep=",") }> result[1] "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9">Thanks. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 http://www.StatisticalEngineering.com ?
Le Jeudi 12 Octobre 2006 12:43, Charles Annis, P.E. a ?crit?:> I have a vector, (not a list) > > > repeated.measures.FACTOR.names > > [1] "Insp1" "Insp2" "Insp3" "Insp4" "Insp5" "Insp6" "Insp7" "Insp8" "Insp9" > > and would like to convert this into a single string > "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" > > I can do that with a loop, but isn't there a more elegant way? > > > result <- repeated.measures.FACTOR.names[[1]] > > for(i in 2:length(repeated.measures.FACTOR.names)) { > > result <- paste(result, repeated.measures.FACTOR.names[[i]], sep=",") } > > > result > > [1] "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" > > > Thanks. > > Charles Annis, P.E. > > Charles.Annis at StatisticalEngineering.com > phone: 561-352-9699 > eFax:? 614-455-3265 > http://www.StatisticalEngineering.compaste() will do what you want. -- Vincent Goulet, Professeur agr?g? ?cole d'actuariat Universit? Laval, Qu?bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
On Thu, 2006-10-12 at 12:43 -0400, Charles Annis, P.E. wrote:> I have a vector, (not a list) > > repeated.measures.FACTOR.names > [1] "Insp1" "Insp2" "Insp3" "Insp4" "Insp5" "Insp6" "Insp7" "Insp8" "Insp9" > > and would like to convert this into a single string > "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" > > I can do that with a loop, but isn't there a more elegant way? > > > result <- repeated.measures.FACTOR.names[[1]] > > for(i in 2:length(repeated.measures.FACTOR.names)) { > result <- paste(result, repeated.measures.FACTOR.names[[i]], sep=",") } > > result > [1] "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" > >paste() is vectorized and note the use of 'collapse' in lieu of 'sep':> paste(repeated.measures.FACTOR.names, collapse = ",")[1] "Insp1,Insp2,Insp3,Insp4,Insp5,Insp6,Insp7,Insp8,Insp9" HTH, Marc Schwartz