1

I have multiple dataframes:

df1    (For a particular sequence1) 
                   
   MeanPosition  Segment Mean
1    Position1   AACCTT  -1.4
2    Position2   AACTCG  -1.2
3    Position3   GCTGAA  -1.1 
4    Position4   CGGACG  -1.5
5    Position5   AACGTA  -1.8
.
.


df2    (For a particular sequence2) 
                
   MeanPosition  Segment Mean
1    Position1   CCAATT  -1.5
2    Position2   ACCTAA  -1.3
3    Position3   GCTGCC  -1.4 
4    Position4   AGGACG  -1.1
5    Position5   AACTTA  -1.2
.
.

df3   (For a particular sequence3)   
                
   MeanPosition  Segment Mean
1    Position1   CCGGTT  -1.6
2    Position2   AGGTAA  -1.7
3    Position3   GAAGCC  -1.5 
4    Position4   AAAACG  -1.3
5    Position5   AACGGA  -1.1
.
.

I want to combine all the dataframes by different sequences that I want to calculate. I want my final outcome to be in another dataframe but in a horizontal format, this format will be further used in extracting various columns for graphical analysis.

            Position1    Position2    Position3   Position4   Position5  .   .   .      
sequence1     -1.4         -1.2         -1.1        -1.5         -1.8    .   .   .      
sequence2     -1.5         -1.3         -1.4        -1.1         -1.2    .   .   .      
sequence3     -1.6         -1.7         -1.5        -1.3         -1.1    .   .   .     

Any help will be much appreciated!

Maharshi
  • 31
  • 3
  • 1
    I think that's https://stackoverflow.com/questions/8169323/r-concatenate-two-dataframes combined with https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format – Martin Gal May 18 '22 at 12:46

1 Answers1

2

You can do this:

pivot_wider(
  bind_rows(df1,df2,df3,.id = "sequence"),
  id_cols = sequence, names_from=MeanPosition,values_from=Mean
)

Output:

  sequence Position1 Position2 Position3 Position4 Position5
  <chr>        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
1 1             -1.4      -1.2      -1.1      -1.5      -1.8
2 2             -1.5      -1.3      -1.4      -1.1      -1.2
3 3             -1.6      -1.7      -1.5      -1.3      -1.1
langtang
  • 22,248
  • 1
  • 12
  • 27