This is a test version of Biostars. For the public version, visit https://www.biostars.org.
change format of table in R. bring the row to coulmn

I have a 22 sample, with another column information about each sample, i want to change the samples to another format as below in output in R. Anyone know how I can do this?

Thank you

 id            position     percent
sample1        1200           30
sample2        1200           20          
sample3        1200           10
....
sample22       1200           40
sample1        1202           50
sample2        1202           30        
sample3        1202           20
....
sample22       1202           60

Output:

  position     sample1  sample2  sample3  ......  sample22
  1200          30        20       10                40
  1202          50        30       20                60
r

pivot_wider() -- the manual will tell you how to use it.

1 answer

You want to transform the dataframe from long- to wide.

library(dplyr) 
library(tidyr) 

your_dataframe %>% 
    pivot_wider(names_from = sample, values_from = position)

Log in to answer this question.