Grouping rows aggregate and function in r

[亡魂溺海] 提交于 2021-01-29 19:01:31

问题


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

results :

  k   n          s           m
1 g   15         3.233333    7.31667 
2 r   11.66667   9           4.16667

This was my attempt :

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) 

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.


回答1:


Here's your function:

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?

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


来源:https://stackoverflow.com/questions/64762654/grouping-rows-aggregate-and-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!