gpt4 book ai didi

r - 如何根据列在 ddply 中传递值?

转载 作者:行者123 更新时间:2023-12-01 10:57:24 24 4
gpt4 key购买 nike

我希望能够传递两组值 GROUPED BY 列 Category。有没有一种方法可以使用 plyr 包中的 ddply 来做到这一点?

我想做这样的事情:

ddply(idata.frame(data), .(Category), wilcox.test, data[Type=="PRE",], data[Type=="POST",])

wilcox.test 是以下函数:

Description

Performs one- and two-sample Wilcoxon tests on vectors of data; the latter is also known as ‘Mann-Whitney’ test.

Usage

wilcox.test(x, ...)

Arguments

x
numeric vector of data values. Non-finite (e.g. infinite or missing) values will be omitted.

y
an optional numeric vector of data values: as with x non-finite values will be omitted.

.... rest of the arguments snipped ....

dput 的输出如下:

structure(list(Category = c("A", "C", 
"B", "C", "D", "E",
"C", "A", "F", "B",
"E", "C", "C", "A",
"C", "A", "B", "H",
"I", "A"), Type = c("POST", "POST",
"POST", "POST", "PRE", "POST", "POST", "PRE", "POST",
"POST", "POST", "POST", "POST", "PRE", "PRE", "POST",
"POST", "POST", "POST", "POST"), Value = c(1560638113,
1283621, 561329742, 2727503, 938032, 4233577690, 0, 4209749646,
111467236, 174667894, 1071501854, 720499, 2195611, 1117814707,
1181525, 1493315101, 253416809, 327012982, 538595522, 3023339026
)), .Names = c("Category", "Type", "Value"), row.names = c(21406L,
123351L, 59875L, 45186L, 126720L, 94153L, 48067L, 159371L, 54303L,
63318L, 104100L, 58162L, 41945L, 159794L, 57757L, 178622L, 83812L,
130655L, 30860L, 24513L), class = "data.frame")

有什么建议吗?

最佳答案

我经常做的是使用匿名函数:

ddply(idata.frame(data), .(Category), 
function(x) wilcox.test(x[Type == "PRE",], x[Type == "POST",])

我不确定 wilcox.test 函数在默认情况下会返回一些很好的连接到 data.frame 中的东西,所以你必须自己调整一下.或者,使用 dlplywilcox.test 输出列表结束。

关于r - 如何根据列在 ddply 中传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com