分组行聚合并在R中起作用

编程入门 行业动态 更新时间:2024-10-25 04:16:47
本文介绍了分组行聚合并在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中起作用

本文发布于:2023-10-17 10:25:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   中起   作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!