I have a dataframe with this data.
我有一个包含这些数据的数据框。
# A tibble: 20 x 3
# Groups: CpG, Group [20]
CpG Group Range
<fct> <dbl> <dbl>
1 cg01003813 1. 0.971
2 cg01003813 2. 0.0917
3 cg01003813 3. 0.0535
4 cg01003813 4. 0.0627
5 cg01003813 5. 0.0575
6 cg26484667 1. 0.0768
7 cg26484667 2. 0.104
8 cg26484667 3. 0.0735
9 cg26484667 4. 0.0453
10 cg26484667 5. 0.139
11 cg25296477 1. 0.0745
12 cg25296477 2. 0.130
13 cg25296477 3. 0.0772
14 cg25296477 4. 0.131
15 cg25296477 5. 0.0765
16 cg13176022 1. 0.0895
17 cg13176022 2. 0.0236
18 cg13176022 3. 0.0412
19 cg13176022 4. 0.0478
20 cg13176022 5. 0.0346
Then I spread the dataframe so that it looks how I would want the matrix to look:
然后我传播数据帧,以便它看起来我希望矩阵看起来:
> tidyr::spread(test, Group, Range, fill = 0)
# A tibble: 4 x 6
# Groups: CpG [4]
CpG `1` `2` `3` `4` `5`
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 cg01003813 0.971 0.0917 0.0535 0.0627 0.0575
2 cg13176022 0.0895 0.0236 0.0412 0.0478 0.0346
3 cg25296477 0.0745 0.130 0.0772 0.131 0.0765
4 cg26484667 0.0768 0.104 0.0735 0.0453 0.1390
Now I'm stuck. I can't figure out how to turn this into a properly formatted and labeled matrix.
现在我被卡住了。我无法弄清楚如何将其转换为格式正确且带标签的矩阵。
I want my 5 groups as the column headers (like they are). I want the CpGs as row names (currently they are the first column. I just can't figure out how to make this into a properly labeled matrix.
我希望我的5个组作为列标题(就像它们一样)。我希望CpG作为行名(目前它们是第一列。我只是无法弄清楚如何将它变成一个正确标记的矩阵。
Thanks for your support.
感谢您的支持。
2 个解决方案
#1
1
just turn it into a matrix like below
只需将其转换为如下所示的矩阵
m <- tidyr::spread(test, Group, Range, fill = 0)
mat <- as.matrix(m[, -1])
dimnames(mat) <- list(m$CpG, colnames(m[, -1]))
mat
1 2 3 4 5
cg01003813 0.9710 0.0917 0.0535 0.0627 0.0575
cg13176022 0.0895 0.0236 0.0412 0.0478 0.0346
cg25296477 0.0745 0.1300 0.0772 0.1310 0.0765
cg26484667 0.0768 0.1040 0.0735 0.0453 0.1390
#2
1
xtabs
gets you closer to what you want:
xtabs让你更接近你想要的:
set.seed(1)
(foo <- cbind(expand.grid(CpG=letters[1:4], Group=1:5), Range = runif(20)))
#> CpG Group Range
#> 1 a 1 0.26550866
#> 2 b 1 0.37212390
#> 3 c 1 0.57285336
#> 4 d 1 0.90820779
#> 5 a 2 0.20168193
#> 6 b 2 0.89838968
#> 7 c 2 0.94467527
#> 8 d 2 0.66079779
#> 9 a 3 0.62911404
#> 10 b 3 0.06178627
#> 11 c 3 0.20597457
#> 12 d 3 0.17655675
#> 13 a 4 0.68702285
#> 14 b 4 0.38410372
#> 15 c 4 0.76984142
#> 16 d 4 0.49769924
#> 17 a 5 0.71761851
#> 18 b 5 0.99190609
#> 19 c 5 0.38003518
#> 20 d 5 0.77744522
xtabs(Range ~ Group + CpG, foo)
#> CpG
#> Group a b c d
#> 1 0.26550866 0.37212390 0.57285336 0.90820779
#> 2 0.20168193 0.89838968 0.94467527 0.66079779
#> 3 0.62911404 0.06178627 0.20597457 0.17655675
#> 4 0.68702285 0.38410372 0.76984142 0.49769924
#> 5 0.71761851 0.99190609 0.38003518 0.77744522
#1
1
just turn it into a matrix like below
只需将其转换为如下所示的矩阵
m <- tidyr::spread(test, Group, Range, fill = 0)
mat <- as.matrix(m[, -1])
dimnames(mat) <- list(m$CpG, colnames(m[, -1]))
mat
1 2 3 4 5
cg01003813 0.9710 0.0917 0.0535 0.0627 0.0575
cg13176022 0.0895 0.0236 0.0412 0.0478 0.0346
cg25296477 0.0745 0.1300 0.0772 0.1310 0.0765
cg26484667 0.0768 0.1040 0.0735 0.0453 0.1390
#2
1
xtabs
gets you closer to what you want:
xtabs让你更接近你想要的:
set.seed(1)
(foo <- cbind(expand.grid(CpG=letters[1:4], Group=1:5), Range = runif(20)))
#> CpG Group Range
#> 1 a 1 0.26550866
#> 2 b 1 0.37212390
#> 3 c 1 0.57285336
#> 4 d 1 0.90820779
#> 5 a 2 0.20168193
#> 6 b 2 0.89838968
#> 7 c 2 0.94467527
#> 8 d 2 0.66079779
#> 9 a 3 0.62911404
#> 10 b 3 0.06178627
#> 11 c 3 0.20597457
#> 12 d 3 0.17655675
#> 13 a 4 0.68702285
#> 14 b 4 0.38410372
#> 15 c 4 0.76984142
#> 16 d 4 0.49769924
#> 17 a 5 0.71761851
#> 18 b 5 0.99190609
#> 19 c 5 0.38003518
#> 20 d 5 0.77744522
xtabs(Range ~ Group + CpG, foo)
#> CpG
#> Group a b c d
#> 1 0.26550866 0.37212390 0.57285336 0.90820779
#> 2 0.20168193 0.89838968 0.94467527 0.66079779
#> 3 0.62911404 0.06178627 0.20597457 0.17655675
#> 4 0.68702285 0.38410372 0.76984142 0.49769924
#> 5 0.71761851 0.99190609 0.38003518 0.77744522