Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reshape dataset in R_ move row to column

I have a dataset with the below format:

enter image description here

structure(list(SNPID = c("SNP_A-1780520", "SNP_A-1780618", "SNP_A-1780632"
), no.1 = c("BB", "AB", "AA"), no.2 = c("BB", "AB", "AA"), no.3 = c("BB", 
"AB", "AB")), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame"))`  

I want to reshape the dataset to have each SNPID as a column and each "no" as a row.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried different R packages but I was not successful to manage this simple task. I appreciate any help. I would like to have someting like this:
enter image description here

>Solution :

An example on reshaping your data with tidyr()

library (tidyr)

df %>% 
  pivot_longer(-SNPID) %>% 
  pivot_wider(names_from = SNPID, values_from = value)

   name  `SNP_A-1780520` `SNP_A-1780618` `SNP_A-1780632` `SNP_A-1780654`
  <chr> <chr>           <chr>           <chr>           <chr>          
1 no.1  BB              AB              AA              AA             
2 no.2  BB              AB              AA              AA             
3 no.3  BB              AB              AA              AA   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading