简体   繁体   中英

Grouping rows aggregate and function in 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM