Hi given the following dataframe
library(tidyverse)
df <- data.frame(READS=rep(c('READa', 'READb', 'READc'),each=3) ,GENE=rep(c('GENEa', 'GENEb', 'GENEc'), each=3), COMMENT=rep(c('CommentA', 'CommentA', 'CommentA'),each=3))
> df
READS GENE COMMENT
1 READa GENEa CommentA
2 READa GENEa CommentA
3 READa GENEa CommentA
4 READb GENEb CommentA
5 READb GENEb CommentA
6 READb GENEb CommentA
7 READc GENEc CommentA
8 READc GENEc CommentA
9 READc GENEc CommentA
I want to convert from long to wide format aggregating by Gene Column so that i get the following
GENEa GENEb GENEc
READSa 3 3 3
READSb 3 3 3
I have tried with no success:
library(tidyverse)
df %>%
group_by(GENE) %>%
select(-COMMENT) %>%
spread(READS)
Note that the original dataframe is huge so any optimized code would help.
Thanks for your help.