Home > OS >  How to apply a function in different ranges of a vectror in R?
How to apply a function in different ranges of a vectror in R?

Time:03-30

I have the following matrix:

x=matrix(c(1,2,2,1,10,10,20,21,30,31,40,
           1,3,2,3,10,11,20,20,32,31,40,
           0,1,0,1,0,1,0,1,1,0,0),11,3)

I would like to find for each unique value of the first column in x, the maximum value (across all records having that value of the first column in x) of the third column in x.

I have created the following code:

v1 <- sequence(rle(x[,1])$lengths)
A=split(seq_along(v1), cumsum(v1==1))
A_diff=rep(0,length(split(seq_along(v1), cumsum(v1==1))))
for( i in 1:length(split(seq_along(v1), cumsum(v1==1))) )
{
A_diff[i]=max(x[split(seq_along(v1), cumsum(v1==1))[[i]],3])
}

However, the provided code works only when same elements are consecutive in the first column (because I use rle) and I use a for loop.

So, how can I do it to work generally without the for loop as well, that is using a function?

CodePudding user response:

If I understand correctly

> tapply(x[,3],x[,1],max)

 1  2 10 20 21 30 31 40 
 1  1  1  0  1  1  0  0 

For grouping more than 1 variable I would do aggregate, note that matrices are cumbersome for this purpose, I would suggest you transform it to a data frame, nonetheless

> aggregate(x[,3],list(x[,1],x[,2]),max)
  • Related