gpt4 book ai didi

r - 如何重新排列数据框以使一列中的值成为行名称?

转载 作者:行者123 更新时间:2023-12-02 19:49:32 26 4
gpt4 key购买 nike

我有一个数据框,其中包含大约 450K 甲基化 beta 值。两个 sample 的 450 个探针。该数据显示在三列中,如下所示:

>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

我想重新排列此 data.frame,以便探针 ID 是行名,样本 ID 是列名,因此它看起来像这样:

>head(ICGC_2)
X932-01-4D X932-01-6D
cg00000029 0.6 0.4
cg00000108 0.3 0.7
cg00000109 0.9 0.1

我已经尝试过:

>library(tidyverse)
ICGC_2 <- ICGC %>% remove_rownames %>% column_to_rownames(var = "probe_id")

但这不起作用,因为 ICGC 中的每个探针 ID 在列中出现两次(因为有两个样本)。我也尝试过:

hello <- data.frame(ICGC[,-2], row.names = ICGC[,2])

但这也有同样的问题。我想以这种方式重新排列此数据的原因是因为我想将 beta 值转换为 M 值并将此数据用作 cpg.annotate 中的对象(可通过 Bioconductor 包 DMRcate 获得) - cpg.annotate 需要该对象将唯一的 Illumina 探针 ID 作为行名称,将唯一的样本 ID 作为列名称。

谢谢!

最佳答案

你们很接近。您需要的是 tidyr 包中的 spread 函数。

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

数据:

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)

关于r - 如何重新排列数据框以使一列中的值成为行名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com