1
b = c(1,1,2,2,3,3,4,4,1)
c = c(10,10,20,20,30,30,40,40,5)
a <- NULL
a  <- matrix(c(b,c), ncol=2)

What I want to do is to compare the numbers In the first column of this matrix, and if the first number is equal to the second consecutive number in the column (in this case if 1 = 1, and so on) then I want to add the corresponding numbers in the second column together (as in 10 + 10 = 20, and so on) and that would be only one value and I want then to store this output in a separate vector.

The output from the matrix I am looking for is as follows:

     [,1] [,2]  [,3]
[1,]    1   10  20 
[2,]    1   10  40
[3,]    2   20  62
[4,]    2   20  85
[5,]    3   30  5
[6,]    3   32
[7,]    4   40
[8,]    4   45
[9,]    1   5  

I am quite new to R and struggling with this. Thank you in advance!

4

2 回答 2

1

这是你想要的吗?我敢打赌,有干净的解决方案,但我在包装中base尝试一下:rollsumzoo

library(zoo)
mm <- cbind(c(1, 1, 2, 2, 3, 3, 4, 4, 1), c(10, 10, 20, 20, 30, 30, 40, 40, 5))

# calculate all lagged sums of column 2
sums <- rollsum(x = mm[ , 2], k = 2)

# calculate differences between consecutive numbers in column 1
diffs <- diff(mm[ , 1])

# select sums where diff is 0, i.e. where the two consecutive numbers in column 1 are equal.
sums2 <- sums[diffs == 0]

sums2
# [1] 20 40 60 80
于 2013-09-06T09:28:51.547 回答
1

这听起来像是rleand的工作tapply

b = c(1,1,2,2,3,3,4,4,1)
c = c(10,10,20,20,30,30,40,40,5)
a <- NULL
a  <- matrix(c(b,c), ncol=2)

A <- rle(a[, 1])$lengths
tapply(a[, 2], rep(seq_along(A), A), sum)
#  1  2  3  4  5 
# 20 40 60 80  5

解释:

  • rle标识矩阵“a”的第一列中项目的运行长度。
  • tapply我们使用 为运行长度创建一个分组变量rep(seq_along(A), A)
  • 我们将这两件事放在一起tapply以获得您想要的总和。
于 2013-09-06T09:39:29.370 回答