R 水平数据帧中三重数据的t检验
我正在尝试做一个循环,这样我就可以测试多个条件:cond_a、cond_B和cond_C,每个条件都针对同一个控件(“ctrl”)。每个条件和控件由三份副本表示。作为结果,我希望得到一个带有条件名称和pvalues的数据帧 以下是我的意见:R 水平数据帧中三重数据的t检验,r,t-test,R,T Test,我正在尝试做一个循环,这样我就可以测试多个条件:cond_a、cond_B和cond_C,每个条件都针对同一个控件(“ctrl”)。每个条件和控件由三份副本表示。作为结果,我希望得到一个带有条件名称和pvalues的数据帧 以下是我的意见: structure(list(ctrl_1 = 1L, ctrl_2 = 2L, ctrl_3 = 3L, cond_A_1 = 4L, cond_A_2 = 4L, cond_A_3 = 4L, cond_B_1 = 5L, cond_B_2 =
structure(list(ctrl_1 = 1L, ctrl_2 = 2L, ctrl_3 = 3L, cond_A_1 = 4L,
cond_A_2 = 4L, cond_A_3 = 4L, cond_B_1 = 5L, cond_B_2 = 5L,
cond_B_3 = 7L, cond_C_1 = 8L, cond_C_2 = 9L, cond_C_3 = 2L), .Names = c("ctrl_1",
"ctrl_2", "ctrl_3", "cond_A_1", "cond_A_2", "cond_A_3", "cond_B_1",
"cond_B_2", "cond_B_3", "cond_C_1", "cond_C_2", "cond_C_3"), class = "data.frame", row.names = c(NA,
-1L))
以及假设PV值的预期输出:
cond_A_pval cond_B_pval cond_C_pval
0.05 0.9 0.006
以下是我的出发点:
pval<-apply(df,1,function(x) {t.test(x[1:3],x[4:6])$p.value})
pval尝试以下操作:
df <- structure(list(ctrl_1 = 1L, ctrl_2 = 2L, ctrl_3 = 3L, cond_A_1 = 4L,
cond_A_2 = 4L, cond_A_3 = 4L, cond_B_1 = 5L, cond_B_2 = 5L,
cond_B_3 = 7L, cond_C_1 = 8L, cond_C_2 = 9L, cond_C_3 = 2L),
.Names = c("ctrl_1", "ctrl_2", "ctrl_3",
"cond_A_1", "cond_A_2", "cond_A_3",
"cond_B_1", "cond_B_2", "cond_B_3",
"cond_C_1", "cond_C_2", "cond_C_3"),
class = "data.frame", row.names = c(NA, -1L))
library(tidyr)
# Reshape the data into key-value pairs.
# It is generally advisable to have data in tidy format.
df <- gather(df)
# Remove the _1, _2, etc.
df$group <- gsub("_\\d", "", df$key)
#Now you can loop through the groups. Note that "ctrl" is the first group:
sapply(unique(df$group)[-1], function(x){
t.test(df[df$group == "ctrl", "value"], df[df$group == x, "value"])$p.value
})
cond_A cond_B cond_C
0.07417990 0.01477836 0.17957429
df使用示例中的值,但不使用我的真实数据,见下文;结构(列表)(键=c(“ctrl_1”、“ctrl_2”、“ctrl_3”、“cond_A_1”、“cond_A_2”、“cond_A_3”、“cond_B_1”、“cond_B_2”、“cond_B_3”、“cond_c_1”、“cond_A_2”、“cond_A_2”、“cond_A_3”)、值=c(“13.382”、“12.9152”、“14.719”、“13.3822”、“12.9152”、“8.788”、“9.3765”、“17.4”、“1526.5”、“cond_2”、“c_2”、“c_3”)、“c组”、“ctrl=c”,“cond_A”,“cond_A”,“cond_B”,“cond_B”,“cond_B”,“cond_C”,“cond_C”,“cond_C”),.Names=C(“key”,“value”,“group”),row.Names=C(NA,-12L),class=“data.frame”)包括:dft这似乎是基础数据的问题。在结构中,这些值被指定为字符(字符串)。很高兴它在将数据转换为数字后工作。知道如何以类似的方式从方差分析中获得pval“Pr(>F)”吗?使用(df,aov(value~group))