使用分组变量按列拆分 data.frame

发布于 2024-11-03 03:24:57 字数 919 浏览 3 评论 0原文

根据分组因素按行分割 data.frame 是相当容易的。但是我如何按列拆分并可能应用函数?

my.df <- data.frame(a = runif(10),
        b = runif(10),
        c = runif(10),
        d = runif(10))
grp <- as.factor(c(1,1, 2,2))

我想要的是按组划分的列的平均值。

到目前为止我所拥有的是一个穷人的申请。

lapply(as.list(as.numeric(levels(grp))), FUN = function(x, cn, data) {
            rowMeans(data[grp %in% x])
        }, cn = grp, data = my.df)

编辑 感谢大家的参与。我运行了 10 次重复*,我的工作 data.frame 大约有 22000 行。这些是几秒钟内的结果。

Roman: 2.19
Joris: 4.60
Joris #2: 3.79 #changed sapply to lapply as suggested by Joris in the [R chatroom][1].
Gavin: 4.70
James & EDi: > 200 # * ran only one replicate due to the large order of magnitude difference

让我感到奇怪的是,手头的任务没有包装函数。也许有一天我们能够做到

apply(X = my.df, MARGIN = 3, INDEX = my.groups, FUN = mean) # :)

It's fairly easy to split a data.frame by rows depending on a grouping factor. But how do I split by columns and possibly apply a function?

my.df <- data.frame(a = runif(10),
        b = runif(10),
        c = runif(10),
        d = runif(10))
grp <- as.factor(c(1,1, 2,2))

What I would like to have is a mean of colums by groups.

What I have so far is a poor man's apply.

lapply(as.list(as.numeric(levels(grp))), FUN = function(x, cn, data) {
            rowMeans(data[grp %in% x])
        }, cn = grp, data = my.df)

EDIT
Thank you all for participating. I ran 10 replicates* and my working data.frame has roughly 22000 rows. These are the results in seconds.

Roman: 2.19
Joris: 4.60
Joris #2: 3.79 #changed sapply to lapply as suggested by Joris in the [R chatroom][1].
Gavin: 4.70
James & EDi: > 200 # * ran only one replicate due to the large order of magnitude difference

It struck me as odd that there is no wrapper function for the task at hand. Maybe someday we'll be able to do

apply(X = my.df, MARGIN = 3, INDEX = my.groups, FUN = mean) # :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风启觞 2024-11-10 03:24:57

您可以使用相同的逻辑,但采用更方便的形式:

sapply(levels(grp),function(x)rowMeans(my.df[which(grp==x)]))

You can use the same logic, but in a more convenient form :

sapply(levels(grp),function(x)rowMeans(my.df[which(grp==x)]))
陪你到最终 2024-11-10 03:24:57

my.df 转换为列表并拆分它,然后在强制转换为数据框后将函数应用于列表组件的每个子集:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

这给出:

> lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

my.df 转换为列表并拆分它,然后在强制转换为数据框后将函数应用于列表组件的每个子集:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

这给出:

1` [1] 0.8229189 0.4901288 0.2057578 0.6531641 0.3897858 0.4225179 [7] 0.3905410 0.3928784 0.1715857 0.3973192

my.df 转换为列表并拆分它,然后在强制转换为数据框后将函数应用于列表组件的每个子集:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

这给出:

2` [1] 0.61348623 0.61229702 0.31938521 0.28325342 0.25857158 [6] 0.49071991 0.01179999 0.57639186 0.38407240 0.17467337

这相当于 @Roman 的“穷人的apply”:

> roman <- lapply(as.list(as.numeric(levels(grp))), 
+                 FUN = function(x, cn, data) {
+                     rowMeans(data[grp %in% x])
+                 }, cn = grp, data = my.df)
> gavin <- lapply(split(as.list(my.df), grp), 
+                 function(x) rowMeans(as.data.frame(x)))
> all.equal(roman, gavin)
[1] "names for current but not for target"

组件上的名称除外。

Convert my.df to a list and split that, then apply your function to each subset of components of the list, after coercing to a data frame:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

This gives:

> lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

Convert my.df to a list and split that, then apply your function to each subset of components of the list, after coercing to a data frame:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

This gives:

1` [1] 0.8229189 0.4901288 0.2057578 0.6531641 0.3897858 0.4225179 [7] 0.3905410 0.3928784 0.1715857 0.3973192

Convert my.df to a list and split that, then apply your function to each subset of components of the list, after coercing to a data frame:

lapply(split(as.list(my.df), grp), function(x) rowMeans(as.data.frame(x)))

This gives:

2` [1] 0.61348623 0.61229702 0.31938521 0.28325342 0.25857158 [6] 0.49071991 0.01179999 0.57639186 0.38407240 0.17467337

Which is equivalent to @Roman's "poor man's apply":

> roman <- lapply(as.list(as.numeric(levels(grp))), 
+                 FUN = function(x, cn, data) {
+                     rowMeans(data[grp %in% x])
+                 }, cn = grp, data = my.df)
> gavin <- lapply(split(as.list(my.df), grp), 
+                 function(x) rowMeans(as.data.frame(x)))
> all.equal(roman, gavin)
[1] "names for current but not for target"

except for the names on the components.

淡紫姑娘! 2024-11-10 03:24:57

这有效吗?

aggregate(t(my.df), list(grp), mean)

Is this working?

aggregate(t(my.df), list(grp), mean)
琉璃繁缕 2024-11-10 03:24:57

怎么样:

my.df2 <- data.frame(t(my.df),grp)
aggregate(.~grp,my.df2,mean)

How about:

my.df2 <- data.frame(t(my.df),grp)
aggregate(.~grp,my.df2,mean)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文