分组行聚合并在R中起作用 [英] Grouping rows aggregate and function in r
本文介绍了分组行聚合并在R中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是r的新手,我想汇总以下矩阵
I am new to r and I wanted to aggregate the following matrix
k n m s
1 g 10 11.8 2.4
2 g 20 15.3 3.2
3 g 15 8.4 4.1
4 r 14 3.0 5.0
5 r 16 6.0 7.0
6 r 5 8.0 15.0
结果:
k n s m
1 g 15 3.233333 7.31667
2 r 11.66667 9 4.16667
这是我的尝试:
k <- c("g", "g", "g", "r","r","r")
n <- c(10,20,15,14,16,5)
m <- c(11.8, 15.3, 8.4,3,6,8)
s <- c(2.4, 3.2, 4.1,5,7,15)
data1 <- data.frame(k,n,m,s)
data2 <- aggregate(m ~ k, FUN = function(t) ********* , data=data1)
我对m更感兴趣,这里是将第一行和第二行除以2(11.8 + 15.30)/2的顺序,然后将结果加到第三行并除以3,依此类推.n和s只是平均值.
I am more interested in m here is the sequence add first and second rows divide by two ( 11.8 + 15.30) / 2 and then add the result to row three and divide by 3 and so on. n and s are just the means.
推荐答案
这是您的函数:
data2 <- aggregate(
m ~ k,
FUN = function(t) sum(t / factorial(length(t)) * factorial(seq_along(t) - 1)),
data=data1)
data2
# k m
# 1 g 7.316667
# 2 r 4.166667
这是一个不寻常的功能,目的是什么?
It's an unusual function, what is it's purpose?
如果要使用其他列的方式,我将使用 dplyr
:
If you want means of the other columns, I'd use dplyr
:
library(dplyr)
data1 %>%
group_by(k) %>%
summarize(
across(c(n, s), mean),
across(m, ~sum(. / factorial(length(.)) * factorial(seq_along(.) - 1)))
)
# # A tibble: 2 x 4
# k n s m
# <chr> <dbl> <dbl> <dbl>
# 1 g 15 3.23 7.32
# 2 r 11.7 9 4.17
这篇关于分组行聚合并在R中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文