0

I have a similar question to counting unique factors in r, however the solutions provided do not work for my data.

My data is organised by drug, with pateint information included in the df as variables as opposed to the patient being the row unit.

               Sex        ID
drug x          F        Jane
drug x          F        Mary
drug x          M        Philip
drug x          F        Jane
drug x          F        Jane
drug x          M        Philip
drug x          M        John
drug x          M        Philip
drug x          F        Jane
drug x          M        Philip

I have 4 unique IDs and I would like to know the gender split across these 4 ids, which should be

F M
2 2

But all the code I try gives me back the number of drugs for each gender e.g,

F M
5 5

Could anybody help me out with this please? When I use the previous help I get a list of unique IDs with sex listed as 50.

Community
  • 1
  • 1
SJS
  • 1
  • Welcome to Stack Overflow! Please add reproducible sample for good people here to help you. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – CHP Mar 11 '13 at 18:37

1 Answers1

2
> df
   sex     id
1    F   Jane
2    F   Mary
3    M Philip
4    F   Jane
5    F   Jane
6    M Philip
7    M   John
8    M Philip
9    F   Jane
10   M Philip

> summary(unique(df[,c('sex','id')])$sex)
F M 
2 2 
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Hi Ananda - thank you so much for your help! I tried your code, but to no avail. My data are actually part of a larger dataframe (80,000 drugs and 100 patients). I made a seperate datframe with just IDs and sex and tried the code you suggested but R tells me "object of type 'closure' is not subsettable". I can't understnd why I can't get R to count the number of F or M in my unique list of identifiers - it seems like a logical thing to do. – SJS Mar 11 '13 at 17:34
  • 2
    @SJS, if this solution isn't working for you (it *should*), why don't you update your question with a bit more accurate sample of your data, some specific code you've tried, and the errors or problems you've encountered in the process. – A5C1D2H2I1M1N2O1R2T1 Mar 11 '13 at 18:31
  • Hi Ananda, thanks for your help with this. Here is an example of my data; > str(m1)[1:5] 'data.frame': 80903 obs. of 50 variables: $ drug_code : int 10020 10020 10020 10020 10020 10020 10020 10020 10020 10020 ... $ sex : Factor w/ 2 levels "F","M": 2 1 1 2 1 1 2 1 1 2 ... $ ID : Factor w/ 100 levels "Patient 1230056694",..: 45 62 62 45 62 62 45 62 62 45 ... here is the code i have tried – SJS Mar 12 '13 at 10:03
  • hang on---- it worked! i used table(unique(m1[c("sex","ID")])$sex). Thank you so much for help and encouragement - as I was writing back to you I tried out more things - so thanks! Looking forward to learning R a bit better so I can help someone out at some stage. Thanks again Ananda. – SJS Mar 12 '13 at 10:33