-3

this is my code

here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time.

i<-2

while (i<=10)
 {
 results<-data.frame()
 results<- t(apply(data,1,function(x) combn(x,i,prod)))
 comb <- combn(colnames(data),i)
 colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2]))
 i<-i+1
 }

now i get the two combination like

V1V2, V1V3,V1V4,....

now i want

 v1v2v3, v1v2v4, ... 

in paste function.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    Please read the help pages > ?paste You are pasting two elements, not three. You can specify three combinations in the combn call and then paste three elements. If you want, as I suspect, all combination of two, three, four and five elements, you will have to find another way to do it. Also, your'e three questions could've been asked in a single one. Please make an effort to be as precise as possible and organize your questions with examples and expected output. – Chargaff Nov 29 '13 at 18:34
  • In your second apply call, the second argument is the margin one, can be 1 or 2 in your case, i goes from 2 to 10. – Chargaff Nov 29 '13 at 18:51
  • This seems to be partly a copy of [**another question**](http://stackoverflow.com/questions/20290069/multiply-multiple-column-and-find-sum-of-each-column-for-multiple-values/20292221#20292221), to which I just provided an answer, but posted with different user names. Hmm... – Henrik Nov 29 '13 at 19:30
  • 4
    Cross-posting is **not** considered good practice. Especially not so when trying to 'hide' it with different user names. – Henrik Nov 29 '13 at 19:36
  • @Chargaff is it possible to extent the values in paste using a loop?(eg:paste(x[1],x[2],x[3]) – user3050374 Nov 29 '13 at 20:02
  • @Henrik sorry its not the same user.actually my friend ask me this question – user3050374 Nov 29 '13 at 20:04
  • Well then, why don't _you_ take a look at the question @vct posted where I provided a solution to the above problem, and then tell your friend the answer. And perhaps tell your friend that you are not interested in cross-posting his questions next time. – Henrik Nov 29 '13 at 20:07
  • @Henrik what you had answered for vct's question is not requirement.i mean only how can be the paste function replaced? – user3050374 Nov 29 '13 at 20:52
  • In my answer I _do_ paste "more than two combinations at the same time", please see the output in the answer. I am sorry but don't find anything in your question asking for "how can be the paste function replaced". Cheers. – Henrik Nov 29 '13 at 20:58
  • @Henrik k.but i couldn't understand how it works.what is that paste0(row,collapse="",row ?? i am new in r. please explain with an example – user3050374 Nov 30 '13 at 08:33

1 Answers1

0

comb <- combn(colnames(data),v)

colnames(results) <- apply(comb,2,function(rows) paste0(rows, collapse = ""))

insted of paste use paste0

@henrik and @chargaff

vct
  • 73
  • 1
  • 10