r - How to rearrange data frame so that values in one column are row names? -
this question has answer here:
- how reshape data long wide format? 7 answers
i have data frame containing 450k methylation beta-values approx. 450 probes 2 samples. data displayed in 3 columns, , looks this:
>head(icgc) submitted_sample_id probe_id methylation_value 1 x932-01-4d cg00000029 0.6 2 x932-01-6d cg00000029 0.4 3 x932-01-4d cg00000108 0.3 4 x932-01-6d cg00000108 0.7 5 x932-01-4d cg00000109 0.9 6 x932-01-6d cg00000109 0.1
i rearrange data.frame probe ids rownames , sample ids column names, looks this:
>head(icgc_2) x932-01-4d x932-01-6d cg00000029 0.6 0.4 cg00000108 0.3 0.7 cg00000109 0.9 0.1
i have tried:
>library(tidyverse) icgc_2 <- icgc %>% remove_rownames %>% column_to_rownames(var = "probe_id")
but didn't work each probe id in icgc appears twice in column (as there 2 samples). tried:
hello <- data.frame(icgc[,-2], row.names = icgc[,2])
but had same problem. reason want rearrange data in way because convert beta values m-values , use data object in cpg.annotate (available through bioconductor package dmrcate) - cpg.annotate requires object have unique illumina probe ids rownames , unique sample ids column names.
thank you!
you close. spread
funciton tidyr
package need.
library(tidyverse) icgc_2 <- icgc %>% spread(submitted_sample_id, methylation_value) %>% remove_rownames() %>% column_to_rownames(var = "probe_id") icgc_2 x932-01-4d x932-01-6d cg00000029 0.6 0.4 cg00000108 0.3 0.7 cg00000109 0.9 0.1
data:
icgc <- read.table(text = "submitted_sample_id probe_id methylation_value 1 'x932-01-4d' cg00000029 0.6 2 'x932-01-6d' cg00000029 0.4 3 'x932-01-4d' cg00000108 0.3 4 'x932-01-6d' cg00000108 0.7 5 'x932-01-4d' cg00000109 0.9 6 'x932-01-6d' cg00000109 0.1", header = true, stringsasfactors = false)
Comments
Post a Comment