在R数据框的选定列之间进行t检验

在R数据框的选定列之间进行t检验,r,plyr,apply,R,Plyr,Apply,我有一个相对简单的问题,我认为我没有正确地使用R 我有一个包含多个观察值的数据框,存储在行中,还有一堆我不想丢失的注释,在同一数据框的其他列中 我想对数据框的几列中的值进行t检验,并将结果写入(理想情况下)同一数据框 一个简单的例子是: # Generate the data experimentName <- paste(rep("name",20), c(1:20), sep="") experimentAnno1 <- rep(paste(rep("anno",5), c(1:

我有一个相对简单的问题,我认为我没有正确地使用R

我有一个包含多个观察值的数据框,存储在行中,还有一堆我不想丢失的注释,在同一数据框的其他列中

我想对数据框的几列中的值进行t检验,并将结果写入(理想情况下)同一数据框

一个简单的例子是:

# Generate the data
experimentName <- paste(rep("name",20), c(1:20), sep="")
experimentAnno1 <- rep(paste(rep("anno",5), c(1:5), sep=""), 4)
a1 <- rnorm(n=20, mean=10, sd=5)
a2 <- rnorm(n=20, mean=11, sd=5)
a3 <- rnorm(n=20, mean=12, sd=5)
b1 <- rnorm(n=20, mean=20, sd=5)
b2 <- rnorm(n=20, mean=21, sd=5)
b3 <- rnorm(n=20, mean=19, sd=5)

sampledata <- cbind(experimentName, experimentAnno1, a1,a2,a3,b1,b2,b3)
这不起作用:(

我还尝试了by()、melt()、apply()等的一系列组合,我认为所有这些组合在某种程度上都是错误的

我希望得到的结果是sampledata数据框中的其他列,它们是:

# pValue
p.value
# LoConf
a$conf.int[1]
# UpConf
a$conf.int[2]
等等

最有效的方法是什么


提前感谢!

您需要先制作
sampledata
a
data.frame
,以获取“a”和“b”列中的数值

>sampledata统计名称(stats)cbind(sampledata,stats)
可能不是最有效的方法,但这里有一种方法是建立在您最初努力的基础上的

您的示例数据:

experimentName <- paste(rep("name",20), c(1:20), sep="")
experimentAnno1 <- rep(paste(rep("anno",5), c(1:5), sep=""), 4)
a1 <- rnorm(n=20, mean=10, sd=5)
a2 <- rnorm(n=20, mean=11, sd=5)
a3 <- rnorm(n=20, mean=12, sd=5)
b1 <- rnorm(n=20, mean=20, sd=5)
b2 <- rnorm(n=20, mean=21, sd=5)
b3 <- rnorm(n=20, mean=19, sd=5)
看起来你们的目标是在每一行中,测试a1,a2,a3的集合,和b1,b2,b3的集合

下面是一些获得这些值的
lappy
函数:

sampledata$pvalue <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$p.value)

sampledata$LoConf <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$conf.int[1])

sampledata$UpConf <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$conf.int[2])

sampledata$pvalue+1这比我的效率更高,打字更少!但是你从哪里得到
测试呢?
> stats <- as.data.frame(do.call(rbind, lapply(1:nrow(sampledata), function(i){
    as.numeric(unlist(t.test(sampledata[i, 3:5], sampledata[i, 6:8]))[1:5])
    })))
> names(stats) <- c("t.stat", "param.df", "p.val", "ci.left", "ci.right")
> cbind(sampledata, stats)
experimentName <- paste(rep("name",20), c(1:20), sep="")
experimentAnno1 <- rep(paste(rep("anno",5), c(1:5), sep=""), 4)
a1 <- rnorm(n=20, mean=10, sd=5)
a2 <- rnorm(n=20, mean=11, sd=5)
a3 <- rnorm(n=20, mean=12, sd=5)
b1 <- rnorm(n=20, mean=20, sd=5)
b2 <- rnorm(n=20, mean=21, sd=5)
b3 <- rnorm(n=20, mean=19, sd=5)
# sampledata <- cbind(experimentName, experimentAnno1, a1,a2,a3,b1,b2,b3)
sampledata <- data.frame(experimentName, experimentAnno1, a1,a2,a3,b1,b2,b3)
sampledata$pvalue <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$p.value)

sampledata$LoConf <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$conf.int[1])

sampledata$UpConf <- sapply(1:nrow(sampledata), function(i) t.test(sampledata[i,c("a1", "a2", "a3")], sampledata[i,c("b1", "b2", "b3")])$conf.int[2])