I have a dataset with the below format:
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.
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