0

i have one large data frame

ID  code    N
1    412    2
1    A341   1
1    A520   16
1    47     9
2    283    4
2    412    3 
.......

the id is rep. and the i want assign the code is 412 598 A333 and the N heve>3 then i can Sort out table like this

ID  code_412    code_598  code_A333
1     0             0         0
2     1             0         0
3     0             1         1

if the ID have code412 3 times or code598 3 times or codeA333 3 times then will key 1 else 0

Vivian
  • 309
  • 1
  • 10
  • 2
    OK, here's some info on [giving a great reproducible example in R](http://stackoverflow.com/a/5963610/2352071). Have a look there, follow the advice and you'll get good answers. Otherwise reposting the same question and deleting the original one won't help you but rather annoy people. – dlaehnemann May 25 '13 at 14:04
  • 2
    Hannah, I am barely willing to help you: Asking multiple questions not showing own efforts, not accepting answers, poor quality of uncomplete questions, deleting a question and reopening it again are the #1 reasons for not getting any help here. However, in this case I wanted to give the answer together with this comment, hoping that you will reconsider it when asking a question again. – Beasterfield May 25 '13 at 14:36
  • I Know it , next time i will be attention. thank you – Vivian May 25 '13 at 15:07

1 Answers1

1

The question is quite unclear. Are the combinations of ID and code unique? Does every combination occur? If not, how should missing combinations be treated?

I assume for the moment, that ID and code are not unique and missing combinations should get a 0 for the according entry. This is a solution using reshape2::dcast:

library( "reshape2" )
mdf <- dcast( mdf,  ID ~ code, value.var = "N",
              fun.aggregate=function(x) ifelse( sum(x) < 3, 0, 1 )
            )
colnames( mdf )[-1] <- paste( "code", colnames(mdf)[-1], sep = "_" )

mdf
ID code_283 code_412 code_47 code_A341 code_A520
1  1        0        0       1         0         1
2  2        1        1       0         0         0
Beasterfield
  • 7,023
  • 2
  • 38
  • 47
  • thanks a lot, each code did not appear in each people.(412 598 A333) is assign. i want to know those people have or no those code. if they have then key 1. but it a good solution – Vivian May 25 '13 at 14:58
  • @Hannah, I'm afraid I am giving up on you. Why don't you just ask "How can I represent existing combinations of `code` and `ID` best?" And still, if my answer does answer your posted question, why don't you accept it? And if it does not answer the question in your mind, why don't you just rephrase it in a way, that everybody understands what you are looking for? – Beasterfield May 25 '13 at 15:07
  • 1
    @Beasterfield although I completely agree with your sentiment, if you find it exasperating to provide help to (clearly) inexperienced users, I suggest not posting at all. We should try to make those with less knowledge and experience better posters of better formulated questions and not make people afraid to post at all. Not everyone's first language is english. – Simon O'Hanlon May 25 '13 at 15:29
  • OH~you say Ticked? sorry i forgot – Vivian May 25 '13 at 15:31
  • @SimonO101 in general I absolutely agree with you. And this is why I answer questions even if I know that they will be closed soon or where obvious simple questions remain unanswered for a couple of hours. Usually I provide my answers then with a well-intentioned hint how to improve the Q next time. But then it is absolutely frustrating just to find yourself with the same person in the same situation over and over again. This is such a case as you can see http://stackoverflow.com/questions/16525846 and there was another Q which has been deleted I think. But yeah, sorry for the overreaction. – Beasterfield May 25 '13 at 15:47
  • @Beasterfield lol, no worries. I am guilty myself too sometimes. :-) – Simon O'Hanlon May 25 '13 at 16:00
  • @Beasterfield sorry i don not know the clicking is represent you accept or no – Vivian May 25 '13 at 16:03