I’ve the following data
mydata <- data.frame(id=c(1,1,1,2,2,3,3,3,3,4,5,5,5), age=c(20,20,20,25,25,19,19,19,19,30,22,22,22), category=c("a","b","c","a","d","a","b","c","d","a","d","b","c"))
I want to reshape it to
ID Age a b c d
1 20 1 1 1 0
2 25 1 0 0 1
3 19 1 1 1 1
4 30 1 0 0 0
5 22 0 1 1 1
Basically I need to add number of binary columns = number of factors in the ‘category’ variable.