Faster ways to calculate frequencies and cast from long to wide

前端 未结 4 919
灰色年华
灰色年华 2020-11-21 04:56

I am trying to obtain counts of each combination of levels of two variables, \"week\" and \"id\". I\'d like the result to have \"id\" as rows, and \"week\" as columns, and t

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 05:36

    You could just use the table command:

    table(data$id,data$week)
    
        1 2 3
      1 2 1 1
      2 0 0 1
    

    If "id" and "week" are the only columns in your data frame, you can simply use:

    table(data)
    #    week
    # id  1 2 3
    #   1 2 1 1
    #   2 0 0 1
    

提交回复
热议问题