Skip to content

Error in combined for() and if() code

6 messages · Nathan Miller, Duncan Murdoch, Uwe Ligges +1 more

#
On 28/12/2010 1:08 PM, Nathan Miller wrote:
When i is at its maximum, i+1 will be beyond the length of data$Phase, 
so you shouldn't use it as an index.

Duncan Murdoch
#
On 28.12.2010 19:08, Nathan Miller wrote:
Nonsense: In this case the loop will only run once for i=length(data$Phase)

you probably want

for(i in seq_along(data$Phase)){
Nonsense:
1. if()... won't return any useful result.
2. i+1 is not within your data

Uwe Ligges
#
On 28/12/2010 1:13 PM, Uwe Ligges wrote:
Yes, I missed that.
if (cond) v1 else v2

does return a value (either v1 or v2).  So the construction

newphase = if (abs(data$Phase[i+1] ....

will set newphase to a new value each time through the loop.  That's 
probably not what was intended...
That's the only one I saw.

Duncan Murdoch
#
On Dec 28, 2010, at 1:08 PM, Nathan Miller wrote:

            
Sometimes it's more informative to look at the data first. Here's a  
plot of the data with the first and second differences underneath>

  plot(data, ylim=c(-5, max(data$Phase)) )
lines(data$Time[-1], diff(data$Phase) )
lines(data$Time[-(1:2)], diff(diff(data$Phase)), col="red")

Your data had rather flat-topped maxima. These maxima are defined by  
the  interval between the times when the first differences are zero  
(OR go from positive to negative)  AND the second differences are  
negative (OR zero).

There is a package on CRAN:

http://cran.r-project.org/web/packages/msProcess/index.html

....  that purports to do peak finding. I would think the local maxima  
in you data might need some filtering and presumably the mass-spec  
people have need of that too.
David Winsemius, MD
West Hartford, CT