0

I have a dataframe like this :

| A_replicate_1 | A_replicate_2 | B_replicate_1 | B_replicate_2 |

How can I modify this dataframe to something like this by stacking A replicates into one column and B replicates into the other column?

|       A       |       B       | 

I tried stacking, but the code became bit too long as I need to stack it and select(-variable) to only have the data I need.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 2
    It looks like you're new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput()` or `reprex::reprex()` and any libraries you are using. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Nov 18 '22 at 16:30
  • 3
    This is most likely a dupe (https://stackoverflow.com/q/68058000/3358272 or https://stackoverflow.com/q/2185252/3358272), but try: `tidyr::pivot_longer(dat, everything(), names_pattern = "([^_]+)_replicate_([0-9]+)", names_to = c(".value", "replicate"))`. – r2evans Nov 18 '22 at 16:33
  • Does this answer your question? [Transforming wide data to long format with multiple variables](https://stackoverflow.com/questions/68058000/transforming-wide-data-to-long-format-with-multiple-variables) – Martin Gal Nov 18 '22 at 16:39

0 Answers0