I'm trying to create a function to find a "local maximas" in every row of my data but ignore if they are not atleast 20% of the "highest" maximum in a row.
Function which I use to find a local maximas:
which(diff(sign(diff(Gene name)))==-2)+1
but I would like to modify it and set pick only if the other maxima is atleast 20% of the highest value.
That's my data:
Name Mo Tue Wen Thu Fr Sat Sun
Mark 0 32 53 11 0 33 52
Ettin 22 51 31 0 0 1 0
Gerard 36 0 13 0 111 33 0
Marcus 0 44 31 10 0 2 0
That's an output I got with my function:
Name Mo Tue Wen Thu Fr Sat Sun
Mark 0 0 1 0 0 0 1 ## Two local maximas
Ettin 0 1 0 0 0 1 0 ## Two local maximas (Should be one!)
Gerard 1 0 1 0 1 0 0 ## Three local maximas (Should be two!)
Marcus 0 1 0 0 0 1 0 ## Two local maximas (Should be one!)
For 3 rows the output isn't correct because the values in cells (Ettin,Sat) & (Gerard, Wen) & (Marcus, Sat) are not even close to atleast 20% of the highest value.
That's what I expect to get with new function:
Name Mo Tue Wen Thu Fr Sat Sun
Mark 0 0 1 0 0 0 1
Ettin 0 1 0 0 0 0 0
Gerard 1 0 0 0 1 0 0
Marcus 0 1 0 0 0 0 0
Is it possible to write such function ?
if(master[j,i]>master[j,i-1]) {
if(master[j,i] > 0.2*max(master [j,])) {
mas_max[j,i] <- 1 ## Setting maxima
mas_max[j,i-1] <- 0 ## Removing potential maxima before
}
}
That's a loop I created but it is not the best way to get desired results.
If your local maxima are at
ind <- which(diff(sign(diff(GeneName)))==-2)+1
then you can get the indices of the thresholds that are no less than 20% of the highest by
ind[GeneName[ind] >= 0.2 * max(GeneName[ind])]
Also, note that ==-2
won't spot local maxima that are part of a plateau, for instance it won't spot c(0,10,10,0) - not sure if that's an issue, but thought it best to point that out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With