-2

hi I am new to r programming I just can't figure out an answer to a problem like I want to change the 2nd column into rows based on the 1st one i.e my datafile looks like

 article  user
  1        u1
  1        u2
  1        u3
  2        u2
  2        u4
  2        u5
  3        u1
  3        u6
  3        u4
  3        u7

so what I want is to form a matrix with rows is the article and column is all user like e.g.

article  u1  u2  u3  u4 u5 u6 u7
 1        1   1   1   0  0  0  0
 2        0   1   0   1  1  0  0
 3        1   0   0   1  0  1  1

thanks for your help in advance.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Naveed Khan Wazir
  • 185
  • 2
  • 4
  • 15

1 Answers1

2

Assuming your data.frame is called "testdf", just use table:

> table(testdf)
       user
article u1 u2 u3 u4 u5 u6 u7
      1  1  1  1  0  0  0  0
      2  0  1  0  1  1  0  0
      3  1  0  0  1  0  1  1

You may also want to use (table(testdf) > 0) * 1 if you expect there might be duplicated combinations and you are only interested in a binary representation.


David's edit, maybe you want?

as.data.frame.matrix(table(testdf))
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • @Jilber sir by applying the above code it is changing the 2nd column into rows but each article is given its matched user , what I want is that All user in the data frame appear on the columns and all article on the rows and if user matched the article ID then 1,and if not 0 as you can see in the table. – Naveed Khan Wazir Jul 16 '14 at 05:54
  • @user3841811, sorry--I don't understand your comment here. – A5C1D2H2I1M1N2O1R2T1 Jul 16 '14 at 06:00
  • actually I want a matrix where all the user in the column ( user/original data frame) appear on column side and all article(article/original file) appear on rows side without duplication and if user matched the column article(original file) then the cell show 1 else 0. I hope I explain well . – Naveed Khan Wazir Jul 16 '14 at 06:24
  • @user3841811, doesn't my suggestion of `(table(testdf) > 0) * 1` do what you're asking for? Are you looking to transpose the output, perhaps? My answer seems to match your expected output directly. – A5C1D2H2I1M1N2O1R2T1 Jul 16 '14 at 07:11
  • no I don't want to transpose the output ,I just want a matrix with binary value for article and user matching.i.e if user matched article then 1 else 0.it will be cross table as you can see in the above example.there are 7 users( u1......u7) for article 1 only u1,u2 and u3 has 1 else 0 similar with article "2" u2,u4 and u5 has 1 else 0. – Naveed Khan Wazir Jul 16 '14 at 07:43
  • 2
    @user3841811, you're on the verge of getting downvoted from me on your question for not being able to provide input and expected output sufficiently. As far as I can tell, `(table(testdf) > 0) * 1` does exactly what you are asking for. I would suggest that you update your question with a better example of the input [***in a reproducible format***](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and output. Your example should include any edge-cases we should be aware of when trying to answer your question. – A5C1D2H2I1M1N2O1R2T1 Jul 16 '14 at 08:21
  • @AnandaMahto, I'm too lazy to read his comments, but maybe he wants `as.data.frame.matrix(table(testdf))` – David Arenburg Jul 16 '14 at 11:02
  • @DavidArenburg, that would be classic, wouldn't it :-) – A5C1D2H2I1M1N2O1R2T1 Jul 16 '14 at 11:04
  • @user3841811 the output of Ananda's code matches your desired output in the question. Is there a problem? – Roman Luštrik Jul 16 '14 at 12:13