change format of table in R. bring the row to coulmn
1
0
Entering edit mode
12 months ago
sata72 • 0

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 • 521 views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
3
Entering edit mode
12 months ago
ponganta ▴ 590

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

library(dplyr) 
library(tidyr) 

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

Login before adding your answer.

Traffic: 1791 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6