0

I have a data set that looks something like this:

id score
a 1
a 5
a 5
b 8
b 3
b 2 
b 9
b 3
b 5
c 3
c 1
d 8
d 3
d 9

I'm looking to reshape the data so that I get a count of each number per variable, something like this:

  1 2 3 4 5 6 7 8 9 
a 1 0 0 0 2 0 0 0 0
b 0 1 2 0 1 0 0 0 1
c 1 0 1 0 0 0 0 0 0
d 0 0 1 0 0 0 0 1 1

I know that ddply can do this using the count(score==1), count(score ==2) function etc., but my actual data set has values from 1-100 and its inefficient to hardcore in 100 different functions for ddply.

Does anyone have a simple way to do this?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Nechama
  • 39
  • 2
  • 2
    Just do `table(data)` after converting the score to `factor` i.e. `table(transform(data, score = factor(score, levels =1:9)))` – akrun Feb 10 '17 at 15:09
  • Another option is `reshape2::dcast(data, id ~ score, length, value.var = "score")` . You need to add the missing score factors as suggested above. – Matteo Castagna Feb 10 '17 at 15:30

0 Answers0