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
CodePudding user response:
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
CodePudding user response:
Base R and as oneliner:
df = 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"))
df_new = as.data.frame(t(df))
Output:
df_new
V1 V2 V3
SNPID SNP_A-1780520 SNP_A-1780618 SNP_A-1780632
no.1 BB AB AA
no.2 BB AB AA
no.3 BB AB AB
CodePudding user response:
This would be a case for data.table::transpose
data.table::transpose(df1, make.names = "SNPID", keep.names = "name")
name SNP_A-1780520 SNP_A-1780618 SNP_A-1780632
1 no.1 BB AB AA
2 no.2 BB AB AA
3 no.3 BB AB AB