Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading