Dou you know how to create a sequence of columns without writing one by one? For example, I woul like to summarise for the states of some country. For example, if my df is
head(df)
Time day State Vote
d1-h1 1 state1 5
d1-h2 1 state1 3
d1-h3 1 state1 8
d2-h1 2 state1 4
d2-h2 2 state1 5
d2-h3 2 state1 1
d1-h1 1 state2 5
d1-h2 1 state2 3
d1-h3 1 state2 8
d2-h1 2 state2 3
d2-h2 2 state2 1
d2-h3 2 state2 4
The one by one case is as follow
df2<- df %>% group_by(Time) %>% summarise(state1 = sum(votes),
state2 = sum(votes),
......
state27 = sum(vote))
One solution for this problem is a loop, but it doesnt work
states<- unique(df$State)
for(i in 1:length(states))
{
df2<- df %>% group_by(Time) %>% summarise( paste0("Vote_",states[i]) = sum(Vote[State == states[i]]))
}