I am working with R.
I have the following data:
v1 <- c("2010-01","2010-02", "2010-03", "2010-04", "2010-05")
v2 <- c("A", "B", "C", "D", "E")
dates <- as.factor(sample(v1, 1000, replace=TRUE, prob=c(0.5, 0.2, 0.1, 0.1, 0.1)))
types <- as.factor(sample(v2,1000, replace=TRUE, prob=c(0.3, 0.2, 0.1, 0.1, 0.1)))
var = rnorm(1000,10,10)
problem_data = data.frame(var,dates, types)
> head(problem_data)
var dates types
1 -6.772497 2010-01 A
2 6.769367 2010-01 D
3 18.914358 2010-02 C
4 6.517997 2010-02 E
5 19.616047 2010-01 B
6 5.129928 2010-01 B
I am trying to make a final data set that contains a new column for each unique "group" within the "types" column. I know how to do this manually:
library(dplyr)
graph_data = data.frame(problem_data %>% group_by(dates, types) %>% summarise(count = n()))
col_A <- graph_data[which(graph_data$types == "A"), ]
col_B <- graph_data[which(graph_data$types == "B"), ]
col_C <- graph_data[which(graph_data$types == "C"), ]
col_D <- graph_data[which(graph_data$types == "D"), ]
col_E <- graph_data[which(graph_data$types == "E"), ]
final_data = data.frame(col_A$dates, col_A$count, col_B$count, col_C$count, col_D$count, col_E$count)
col_A.dates col_A.count col_B.count col_C.count col_D.count col_E.count
1 2010-01 189 130 57 58 53
2 2010-02 63 62 25 18 30
3 2010-03 46 24 12 12 11
4 2010-04 45 17 8 16 15
5 2010-05 42 26 13 12 16
Is there a more direct way to do this in R?
Thanks!