下面提供的第一个数据集称为“ pcr_plate”,是96孔PCR板的格式。
第二个数据集,称为“ sample_transposed_plate”,是所需结果的一个示例。
我尝试了使用以下软件包中的代码来转置数据的经典方法:
图书馆(dplyr) 图书馆(tidyr)
除了transform(data.frame,...)
但是,我对如何编写从行A列1:12开始,然后是行B列1:12等的代码感到困惑,同时又将相应的样本(也称为井)保留在相邻列中>
两个示例工作数据集:
pcr_plate <- read.table ("https://pastebin.com/raw/bGhzj5dU", header = T, sep = "")
sample_transposed_plate <- read.table ("https://pastebin.com/raw/xyzv9cvH", header=T, sep = "") # example of desired outcome
从逻辑上讲,给定有96孔,最后我应该有96行数据(带有标题的97行)。第一列为“位置”,第二列为“样本”
答案 0 :(得分:1)
您只需使用典型的gather
即可将宽数据转换为长数据:
library(tidyverse)
pcr_plate <- read.table ("https://pastebin.com/raw/bGhzj5dU", header = T, sep = "")
sample_transposed_plate <- read.table ("https://pastebin.com/raw/xyzv9cvH", header=T, sep = "")
pcr_plate_long = pcr_plate %>%
gather(number, content, -pcr.plate) %>%
mutate(number = str_remove(number, "^X"),
well_ID = paste0(pcr.plate, number)) %>%
select(content, well_ID)
答案 1 :(得分:1)
这是您要寻找的(下)
well_ID
中的数字排列数据的方法。 让我知道这是否无效或您有任何疑问。
pcr_plate <- read.table ("https://pastebin.com/raw/bGhzj5dU", header = T, sep = "")
library(dplyr)
library(tidyr)
gather(pcr_plate, key = "key", value = "sample", -pcr.plate) %>%
mutate(pcr.plate = paste0(pcr.plate, gsub("X", "", key))) %>%
select(well_ID = pcr.plate, sample) %>%
arrange(gsub("\\d", "", well_ID, as.numeric(gsub("\\D", "", well_ID))))
well_ID sample
1 A1 1-TypA
2 A2 1-TypA
3 A3 2-TypA
4 A4 2-TypA
5 A5 3-TypA
6 A6 3-TypA
7 A7 4-TypA
8 A8 4-TypA
9 A9 H20-TypA
10 A10 H20-TypA
11 A11 PosA-TypA
12 A12 PosB-RP49
13 B1 1-TypB
14 B2 1-TypB
15 B3 2-TypB
16 B4 2-TypB
17 B5 3-TypB
18 B6 3-TypC
19 B7 4-TypB
20 B8 4-TypB
21 B9 H20-TypB
22 B10 H20-TypB
23 B11 PosA-TypA
24 B12 PosB-RP49
25 C1 1-TypC
26 C2 1-TypC
27 C3 2-TypC
28 C4 2-TypC
29 C5 3-TypC
30 C6 3-TypB
31 C7 4-TypC
32 C8 4-TypC
33 C9 H20-TypC
34 C10 H20-TypC
35 C11 PosA-RP49
36 C12 PosB-H20
37 D1 1-RPS5
38 D2 1-RPS5
39 D3 2-RPS5
40 D4 2-RPS5
41 D5 3-RPS5
42 D6 3-RPS5
43 D7 4-RPS5
44 D8 4-RPS5
45 D9 H20-RPS5
46 D10 H20-RPS5
47 D11 PosA-RP49
48 D12 PosB-H20
49 E1 1-RP49
50 E2 1-RP49
51 E3 2-RP49
52 E4 2-RP49
53 E5 3-RP49
54 E6 3-RP49
55 E7 4-RP49
56 E8 4-RP49
57 E9 H20-RP49
58 E10 H20-RP49
59 E11 PosA-H20
60 E12 H20
61 F1 1-F1430A
62 F2 1-F1430A
63 F3 2-F1430A
64 F4 2-F1430A
65 F5 3-F1430A
66 F6 3-F1430A
67 F7 4-F1430A
68 F8 4-F1430A
69 F9 H20-F1430A
70 F10 H20-F1430A
71 F11 PosA-H20
72 F12 H20
73 G1 1-F1409B
74 G2 1-F1409B
75 G3 2-F1409B
76 G4 2-F1409B
77 G5 3-F1409B
78 G6 3-F1409B
79 G7 4-F1409B
80 G8 4-F1409B
81 G9 H20-F1409B
82 G10 H20-F1409B
83 G11 PosB-TypA
84 G12 H20
85 H1 1-H20
86 H2 1-H20
87 H3 2-H20
88 H4 2-H20
89 H5 3-H20
90 H6 3-H20
91 H7 4-H20
92 H8 4-H20
93 H9 H20-H20
94 H10 H20-H20
95 H11 PosB-TypA
96 H12 H20
编辑:
对我的评论不正确,它确实按预期工作。尽管可以获得相同的结果,但这是另一种方法。让我知道您是否有疑问!
gather(pcr_plate, key = "key", value = "sample", -pcr.plate) %>%
mutate(key = as.numeric(gsub("X", "", key))) %>%
arrange(pcr.plate, key) %>%
mutate(pcr.plate = paste0(pcr.plate, key)) %>%
select(well_ID = pcr.plate, sample)