5

考虑以下数据框:

df <- data.frame(row_id = c("r1","r2","r3","r4","r1","r2","r3","r4"),
                 v1 = c(3,2,5,2,5,2,6,4),
                 v2 = c(4,3,5,3,7,4,6,7))

我想用“row_id”取和积。也就是说,对于具有 row_id:"r1" 的行,我想做以下计算:(3*4)+(5*7)。等等。

因此,我最终将得到以下矩阵:

df1 <- data.frame(row_id = c("r1","r2","r3","r4"),
                 v1 = c(47,14,61,34))

任何帮助将不胜感激。

谢谢。

4

5 回答 5

6

类似但略短:

dplyr::count(df, row_id, wt = v1*v2)
于 2019-10-15T17:39:08.820 回答
4

使用基数 R,我们也transform可以aggregate

 aggregate(tot~row_id,transform(df,tot = v1*v2),sum)

  row_id tot
1     r1  47
2     r2  14
3     r3  61
4     r4  34

或者你也可以这样做:

c(by(df[-1],df[1],do.call,what = "%*%"))
r1 r2 r3 r4 
47 14 61 34 
于 2019-10-15T17:43:08.253 回答
3
library(dplyr)
df %>%
    mutate(p = Reduce("*", .[-1])) %>%
    group_by(row_id) %>%
    summarise(v = sum(p))

或者

tapply(Reduce("*", df[-1]), df$row_id, sum)
#r1 r2 r3 r4 
#47 14 61 34 
于 2019-10-15T17:37:07.120 回答
3

base Rsplit和一起使用%*%

sapply(split(df[-1], df$row_id), function(x) x[,1] %*% x[,2])
# r1 r2 r3 r4 
#47 14 61 34 

或者另一个选项rowsum来自base R

rowsum(with(df, v1 * v2), group = df$row_id)
#    [,1]
#r1   47
#r2   14
#r3   61
#r4   34

或使用data.table

library(data.table)
setDT(df)[, do.call(`%*%`, .SD), row_id]
#   row_id V1
#1:     r1 47
#2:     r2 14
#3:     r3 61
#4:     r4 34
于 2019-10-15T17:42:27.697 回答
1

使用dplyr

library(dplyr)
df %>% group_by(row_id) %>% summarize(sum(v1*v2))

# which gives:
# A tibble: 4 x 2
  row_id `sum(v1 * v2)`
  <fct>           <dbl>
1 r1                 47
2 r2                 14
3 r3                 61
4 r4                 34
于 2019-10-16T05:05:30.500 回答