1

I have an R problem. I would like to count factors of a variable depending of an other variable in a dataframe.

I give you an example:

I have

    ID   com
    125 nul
    125 nul
    125 dec
    125 asc
    125 0
    130 nul
    130 dec

What I want

    ID|nul|dec|asc|0
    125|2|1|1|1
    130|1|1|0|0

NB: the variable com is a factor, and ID is intger.

I tried the easy way that I know: table(df$ID, df$com) but it hasn't worked.

Marie
  • 127
  • 1
  • 9

1 Answers1

4

You can try dcast

library(reshape2)
dcast(df,ID~com, value.var='com', length)
#   ID nul dec asc 0
#1 125   2   1   1 1
#2 130   1   1   0 0

Or just use table

 table(df)
 #    nul dec asc 0
 #125   2   1   1 1
 #130   1   1   0 0

data

df <- structure(list(ID = c(125L, 125L, 125L, 125L, 125L, 130L, 130L
 ), com = structure(c(1L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("nul", 
"dec", "asc", "0"), class = "factor")), .Names = c("ID", "com"
), row.names = c(NA, -7L), class = "data.frame")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you, I used dcast and it works!! – Marie Jul 09 '15 at 13:43
  • @Marie Thanks for the feedback. I don't understand why the table command didn't work for you. Saw your code after I posted the solution. – akrun Jul 09 '15 at 13:46
  • The table command didn't work because it gave me the good result but in a way that I didn't want. I had the ID variable, the com variable and the frequence. What I wanted was each factors become a variable. – Marie Jul 09 '15 at 13:52
  • @Marie May be you had unused levels in the `com` column after subsetting. i.e. `table(droplevels(df))` would work. (It is hard to understand from the comments as the formatting is not good) – akrun Jul 09 '15 at 13:52