我有兩個數據幀:如何從兩個數據分組值之間進行操作框架
src_tbl <- structure(list(Sample_name = c("S1", "S2", "S1", "S2", "S1",
"S2"), crt = c(0.079, 0.082, 0.079, 0.082, 0.079, 0.082), sr = c(0.592,
0.549, 0.592, 0.549, 0.592, 0.549), condition = c("x1", "x1",
"x2", "x2", "x3", "x3"), score = c("0.077", "0.075", "0.483",
"0.268", "0.555", "0.120")), row.names = c(NA, -6L), .Names = c("Sample_name",
"crt", "sr", "condition", "score"), class = c("tbl_df",
"tbl", "data.frame"))
src_tbl
#> Sample_name crt sr condition score
#> 1 S1 0.079 0.592 x1 0.077
#> 2 S2 0.082 0.549 x1 0.075
#> 3 S1 0.079 0.592 x2 0.483
#> 4 S2 0.082 0.549 x2 0.268
#> 5 S1 0.079 0.592 x3 0.555
#> 6 S2 0.082 0.549 x3 0.120
ref_tbl <- structure(list(Sample_name = c("P1", "P2", "P3", "P1", "P2",
"P3", "P1", "P2", "P3"), crt = c(1, 1, 1, 1, 1, 1, 1, 1, 1),
sr = c(2, 2, 2, 2, 2, 2, 2, 2, 2), condition = c("r1", "r1",
"r1", "r2", "r2", "r2", "r3", "r3", "r3"), score = c("0.200",
"0.201", "0.199", "0.200", "0.202", "0.200", "0.200", "0.204",
"0.197")), row.names = c(NA, -9L), .Names = c("Sample_name",
"crt", "sr", "condition", "score"), class = c("tbl_df",
"tbl", "data.frame"))
ref_tbl
#> Sample_name crt sr condition score
#> 1 P1 1 2 r1 0.200
#> 2 P2 1 2 r1 0.201
#> 3 P3 1 2 r1 0.199
#> 4 P1 1 2 r2 0.200
#> 5 P2 1 2 r2 0.202
#> 6 P3 1 2 r2 0.200
#> 7 P1 1 2 r3 0.200
#> 8 P2 1 2 r3 0.204
#> 9 P3 1 2 r3 0.197
我想要做的是執行對分組score
列操作(ks.test()
)在兩個數據幀中均爲Sample_name
。例如KS檢驗S1和P1的p值:
# in src_tbl
s1 <- c(0.077,0.483,0.555)
#in ref_tbl
p1 <- c(0.200,0.200,0.200)
testout <- ks.test(s1,p1)
#> Warning in ks.test(s1, p1): cannot compute exact p-value with ties
broom::tidy(testout)
#> statistic p.value method alternative
#> 1 0.6666667 0.5175508 Two-sample Kolmogorov-Smirnov test two-sided
我想這樣做是爲了讓最終,我們得到的表像,以對所有的操作全部執行這
src ref p.value
S1 P1 0.5175508
S1 P2 0.6
S1 P3 0.6
S2 P1 0.5175508
S2 P2 0.6
S2 P3 0.6
我該怎麼做?由於ref_table
中的樣本數可能很大(P1,P2 .... P10k),所以優先選擇較快。
兩個數據幀的長度是不一樣的嗎? –
@ J.con'src_tbl'和'ref_tbl'可以具有相同或不同的維度。 – pdubois