I have a data frame that looks something like this (see link). I would like to draw a conclusion, which will be created below, and take another step, spreading the tonality variable in both n and the middle variables. It looks like this topic can carry it, but I can't get it to work:
Is it possible to use spread over several columns in a tidyr like dcast?
I would like the destination table to have the source variable in one column, then the variables tone-n and tone-avg were in the columns. Therefore, I would like the column headings to be "source" - "For-n" - "Against-n" "For -Avg" - "Against-Avg". This is for publication, not for further calculations, therefore it concerns the presentation of data. It seems to me more intuitive to present data in this way. Thank.
Politician.For<-sample(seq(0,4,1),50, replace=TRUE)
Politician.Against<-sample(seq(0,4,1),50, replace=TRUE)
Activist.For<-sample(seq(0,4,1),50,replace=TRUE)
Activist.Against<-sample(seq(0,4,1),50,replace=TRUE)
df<-data.frame(Politician.For, Politician.Against, Activist.For,Activist.Against)
df %>%
gather(df) %>%
separate(col=df, into=c('source', 'tone')) %>%
group_by(source,tone) %>%
summarise(n=sum(value), avg=mean(value)) %>%
spread(tone, c('n', 'value'))