R将来自大量汇总表的变量存储在新文档的列中
R Store variables from large number of summary tables in columns of new document
我有一个包含 6 列的数据框 (X),名称为:mean.x、s.x、n.x、mean.y、s.y、n.y.它们代表总体 x 和 y 的均值、st dev (s) 和样本量 (n)。我是 运行 一个根据这些统计参数执行 t 检验的 R 包 (BSDA)。问题是对于每一行我得到 1 个摘要 table 并且我有 640.000 行。
我想要做的是使用 640.000 摘要 table 中的所有 p 值和其他参数创建新列。这可能吗?
前 5 行的值相同:mean.x (0.444357)、s.x (0.02575427)、n.x (633744)、mean.y (0.4308 ), s.y (0.000628747), n.y (390)
这是显示摘要的脚本 tables:
library(BSDA)
tsum.test(mean.x = X$mean.x,
s.x = X$s.x,
n.x = X$n.x,
mean.y = X$mean.y,
s.y = X$s.y,
n.y = X$n.y,
alternative = "less",
mu = 0, # null hypothesis that there is no diff between means
var.equal = FALSE,
conf.level = 0.95)
非常感谢!
有可能。看看下面。一种方法是为此使用 apply
:
想象一个非常简单的 data.frame
(对于这个简单示例,所有行都相同):
x <- c(7.8, 6.6, 6.5, 7.4, 7.3, 7.0, 6.4, 7.1, 6.7, 7.6, 6.8)
y <- c(4.5, 5.4, 6.1, 6.1, 5.4, 5.0, 4.1, 5.5)
X <- data.frame(mean_x = mean(x), s.x = sd(x), n.x = 11, mean_y = mean(y), s.y = sd(y),
n.y = 8)
X <- rbind(X, X, X)
#> X
# mean_x s.x n.x mean_y s.y n.y
#1 7.018182 0.4643666 11 5.2625 0.7069805 8
#2 7.018182 0.4643666 11 5.2625 0.7069805 8
#3 7.018182 0.4643666 11 5.2625 0.7069805 8
然后你在每一行使用 apply
到 运行 你的 tsum.test
并提取你需要的参数。例如,我提取了 p.values
和 degrees of freedom
:
new_cols <-
apply(X, 1, function(x) {
#using apply in each iteration, a row will be fed to the tsum.test function
#so make sure you re using the correct ones
stats <-
#x[1] corresponds to the first column, x[2] to the second and so on
tsum.test(mean.x = x[1],
s.x = x[2],
n.x = x[3],
mean.y = x[4],
s.y = x[5],
n.y = x[6],
alternative = "less",
mu = 0, # null hypothesis that there is no diff between means
var.equal = FALSE,
conf.level = 0.95)
#output p.values and degrees of freedom on this occasion
c(pvalue = stats$p.value, df = stats$parameters)
})
以上输出自由度和p.values,为了绑定到你的data.frame你可以这样做:
> cbind(X, t(new_cols))
mean_x s.x n.x mean_y s.y n.y pvalue.mean_x df.df
1 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292
2 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292
3 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292
我有一个包含 6 列的数据框 (X),名称为:mean.x、s.x、n.x、mean.y、s.y、n.y.它们代表总体 x 和 y 的均值、st dev (s) 和样本量 (n)。我是 运行 一个根据这些统计参数执行 t 检验的 R 包 (BSDA)。问题是对于每一行我得到 1 个摘要 table 并且我有 640.000 行。
我想要做的是使用 640.000 摘要 table 中的所有 p 值和其他参数创建新列。这可能吗?
前 5 行的值相同:mean.x (0.444357)、s.x (0.02575427)、n.x (633744)、mean.y (0.4308 ), s.y (0.000628747), n.y (390)
这是显示摘要的脚本 tables:
library(BSDA)
tsum.test(mean.x = X$mean.x,
s.x = X$s.x,
n.x = X$n.x,
mean.y = X$mean.y,
s.y = X$s.y,
n.y = X$n.y,
alternative = "less",
mu = 0, # null hypothesis that there is no diff between means
var.equal = FALSE,
conf.level = 0.95)
非常感谢!
有可能。看看下面。一种方法是为此使用 apply
:
想象一个非常简单的 data.frame
(对于这个简单示例,所有行都相同):
x <- c(7.8, 6.6, 6.5, 7.4, 7.3, 7.0, 6.4, 7.1, 6.7, 7.6, 6.8)
y <- c(4.5, 5.4, 6.1, 6.1, 5.4, 5.0, 4.1, 5.5)
X <- data.frame(mean_x = mean(x), s.x = sd(x), n.x = 11, mean_y = mean(y), s.y = sd(y),
n.y = 8)
X <- rbind(X, X, X)
#> X
# mean_x s.x n.x mean_y s.y n.y
#1 7.018182 0.4643666 11 5.2625 0.7069805 8
#2 7.018182 0.4643666 11 5.2625 0.7069805 8
#3 7.018182 0.4643666 11 5.2625 0.7069805 8
然后你在每一行使用 apply
到 运行 你的 tsum.test
并提取你需要的参数。例如,我提取了 p.values
和 degrees of freedom
:
new_cols <-
apply(X, 1, function(x) {
#using apply in each iteration, a row will be fed to the tsum.test function
#so make sure you re using the correct ones
stats <-
#x[1] corresponds to the first column, x[2] to the second and so on
tsum.test(mean.x = x[1],
s.x = x[2],
n.x = x[3],
mean.y = x[4],
s.y = x[5],
n.y = x[6],
alternative = "less",
mu = 0, # null hypothesis that there is no diff between means
var.equal = FALSE,
conf.level = 0.95)
#output p.values and degrees of freedom on this occasion
c(pvalue = stats$p.value, df = stats$parameters)
})
以上输出自由度和p.values,为了绑定到你的data.frame你可以这样做:
> cbind(X, t(new_cols))
mean_x s.x n.x mean_y s.y n.y pvalue.mean_x df.df
1 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292
2 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292
3 7.018182 0.4643666 11 5.2625 0.7069805 8 0.9999669 11.30292