简体   繁体   English

按R中的行组求和

[英]Summing by groups of rows in R

This is a bit of a difficult question to title, so edits welcome. 这是一个很难回答的标题问题,因此欢迎编辑。 The data looks like this: 数据如下所示:

mat =         

     [,1]
 [1,] 9.586352e-04
 [2,]           NA
 [3,] 2.605841e-03
 [4,] 7.868957e-05
 [5,] 1.000000e+00
 [6,]           NA
 [7,] 8.208500e-02
 [8,] 2.605841e-03
 [9,] 7.868957e-05
[10,] 1.000000e+00
[11,] 9.586352e-04
[12,] 8.208500e-02
[13,] 2.605841e-03
[14,] 7.868957e-05
[15,] 1.000000e+00

I want to sum every 5 elements, so since there are 15, I the length of the returned vector should be 3. (15/3). 我想对每5个元素求和,所以既然有15个元素,则返回的向量的长度应为3。(15/3)。 So for example, just count the NA's as 0. 因此,例如,仅将NA计数为0。

How do I do this? 我该怎么做呢?

I also want to ignore the NA's 我也想忽略NA

m <- matrix(1:15, ncol = 1)
m[cbind(c(3,7),c(1, 1))] <- NA

library(zoo)
rollapply(m, sum, width = 5, by = 5, na.rm = TRUE)
     [,1]
[1,]   12
[2,]   33
[3,]   65

You could use tapply() 您可以使用tapply()

mat <- matrix(c(1, 2, NA, 4:6, NA, 8:15))
## set up a grouping vector
grp <- rep(1:(nrow(mat)/5), each = 5)
## compute group sums
tapply(mat, grp, sum, na.rm = TRUE)
#  1  2  3 
# 12 33 65   

A less efficient option involves split() with vapply() 效率较低的选项涉及split()vapply()

vapply(split(mat, grp), sum, 1, na.rm = TRUE)
#  1  2  3 
# 12 33 65 

This is ideal for ?rowsum , which should be fast 这对于?rowsum是理想的,它应该很快

Using RStudent's data 使用RStudent的数据

rowsum(m, rep(1:3, each=5), na.rm=TRUE)

The second argument, group , defines the rows which to apply the sum over. 第二个参数group定义了将总和应用到的行。 More generally, the group argument could be defined rep(1:nrow(m), each=5, length=nrow(m)) (sub nrow with length if applying over a vector) 更一般而言,可以将组参数定义为rep(1:nrow(m), each=5, length=nrow(m)) (如果应用于向量,则以lengthnrow

Using dplyr 使用dplyr

library(dplyr)
mat <- matrix(c(1, 2, NA, 4:6, NA, 8:15))
df <- data.frame(mat)

df %>%
  mutate(group = rep(1:(n()/5), each=5)) %>%
  group_by(group) %>%
  summarise(mat = sum(mat, na.rm = TRUE))

You get: 你得到:

#Source: local data frame [3 x 2]

#  group mat
#1     1  12
#2     2  33
#3     3  65

If, for some reasons, you would like to replace NA s by 0 (because you want to perform some other operations than a sum() , say a mean() ) you can do: 如果由于某些原因,您想将NA替换为0(因为您想要执行除sum()之外的其他操作,例如mean() ),则可以执行以下操作:

df %>%
  mutate(mat = ifelse(is.na(mat), 0, mat)) %>%
  mutate(group = rep(1:(n()/5), each=5)) %>%
  group_by(group) %>%
  summarise(mat = mean(mat))

You'll get the result with NA s equal to 0 (instead of omitting NA with na.rm = TRUE in previous suggestion) 您将获得NA等于0的结果(而不是在先前的建议中省略na.rm = TRUE NA

#Source: local data frame [3 x 2]

#  group  mat
#1     1  2.4
#2     2  6.6
#3     3 13.0

The sum function has an na.rm option. sum函数具有na.rm选项。

dfsum <- numeric()
i <- 1
j <- 1
while (i < nrow(df)) { 
    dfsum[j] <- sum(df[i,2] : df [i+4,2], na.rm=TRUE)
    i <- i+5
    j <- j+ 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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