r - Using spread to create two value columns with tidyr -


i have data frame looks (see link). i'd take output produced below , go 1 step further spreading tone variable across both n , average variables. seems topic might bear on this, can't work: is possible use spread on multiple columns in tidyr similar dcast?

i'd final table have source variable in 1 column, then tone-n , tone-avg variables in columns. i'd column headers "source" - "for - n" - "against - n" "for -avg" - "against - avg". publication, not further calculation, it's presenting data. seems more intuitive me present data in way. thank you.

#variable1 politician.for<-sample(seq(0,4,1),50, replace=true) #variable2 politician.against<-sample(seq(0,4,1),50, replace=true) #variable3 activist.for<-sample(seq(0,4,1),50,replace=true) #variable4 activist.against<-sample(seq(0,4,1),50,replace=true) #dataframe df<-data.frame(politician.for, politician.against, activist.for,activist.against)  #tidyr df %>%  #gather columns   gather(df) %>%  #separate period character   #(default separation character non-alpha numeric characterr)   separate(col=df, into=c('source', 'tone')) %>%  #group both source , tone    group_by(source,tone) %>%  #summarise create counts , average  summarise(n=sum(value), avg=mean(value)) %>%  #try spread  spread(tone, c('n', 'value')) 

i think want gather break out count , mean separate observations, gather(type, val, -source, -tone) below.

gather(df, who, value) %>%     separate(who, into=c('source', 'tone')) %>%     group_by(source, tone) %>%     summarise(n=sum(value), avg=mean(value)) %>%     gather(type, val, -source, -tone) %>%     unite(stat, c(tone, type)) %>%     spread(stat, val) 

yields

source: local data frame [2 x 5]        source against_avg against_n for_avg for_n 1   activist        1.82        91    1.84    92 2 politician        1.94        97    1.70    85 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -