0

I'm sure this question has been asked before, but I can't seem to find an answer anywhere, so I apologize if this is a duplicate.

I'm looking for R code that allows me to aggregate a variable in R, but while doing so creates new columns that count instances of levels of a factor.

For example, let's say I have the data below:

Week Var1
1       a
1       b
1       a
1       b
1       b
2       c
2       c
2       a
2       b
2       c
3       b
3       a
3       b
3       a

First, I want to aggregate by week. I'm sure this can be done with group_by in dplyr. I then need to be able to cycle through the code and create a new column each time a new level appears in Var 1. Finally, I need counts of each level of Var1 within each week. Note that I can probably figure out a way to do this manually, but I'm looking for an automated solution as I will have thousands of unique values in Var1. The result would be something like this:

 Week   a   b   c
   1    2   3   0
   2    1   1   3
   3    2   2   0
RH2015
  • 141
  • 6

1 Answers1

2

I think from the way you worded your question, you've been looking for the wrong thing/something too complicated. It's a simple data-reshaping problem, and as such can be solved with reshape2:

library(reshape2)

#create wide dataframe (from long)
res <- dcast(Week~Var1, value.var="Var1",
             fun.aggregate = length, data=data)
> res
  Week a b c
1    1 2 3 0
2    2 1 1 3
3    3 2 2 0
Heroka
  • 12,889
  • 1
  • 28
  • 38