[R] Error in combined for() and if() code

David Winsemius dwinsemius at comcast.net
Tue Dec 28 19:49:38 CET 2010


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

> Hello,
>
> I am trying to filter a data set like below so that the peaks in the  
> Phase
> value are more obvious and can be identified by a peak finding  
> function
> following the useful advise of Carl Witthoft. I have written the  
> following
>
> for(i in length(data$Phase)){
> newphase=if(abs(data$Phase[i+1]-data$Phase[i])>6){
> data$Phase[i+1]
> }else{data$Phase[i]
> }
> }
>
> I get the following error which I have not seen before when I paste  
> the code
> into R
>
> Error in if (abs(data$Phase[i + 1] - data$Phase[i]) > 6) { :
>  missing value where TRUE/FALSE needed
>
> I don't have much experience with such loops as I have tried to  
> avoid using
> them in the past. Can anyone identify the error(s) in the code I have
> written or a simpler means of writing such a filter?

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.




>
> Thank you,
> Nate
>
>
> data=
>    Time Phase
> 1  0.000 15.18
> 2  0.017 13.42
> 3  0.034 11.40
> 4  0.051 18.31
> 5  0.068 25.23
> 6  0.085 33.92
> 7  0.102 42.86
> 8  0.119 42.87
> 9  0.136 42.88
> 10 0.153 42.88
> 11 0.170 42.87
> 12 0.186 42.88
> 13 0.203 42.88
> 14 0.220 42.78
> 15 0.237 33.50
> 16 0.254 24.81
> 17 0.271 17.20
> 18 0.288 10.39
> 19 0.305 13.97
> 20 0.322 16.48
> 21 0.339 14.75
> 22 0.356 20.80
> 23 0.373 25.79
> 24 0.390 31.25
> 25 0.407 39.89
> 26 0.423 40.04
> 27 0.440 40.05
> 28 0.457 40.05
> 29 0.474 40.05
> 30 0.491 40.05
> 31 0.508 40.06
> 32 0.525 40.07
> 33 0.542 32.23
> 34 0.559 23.90
> 35 0.576 17.86
> 36 0.592 11.63
> 37 0.609 12.78
> 38 0.626 13.12
> 39 0.643 10.93
> 40 0.660 10.63
> 41 0.677 10.82
> 42 0.694 11.84
> 43 0.711 20.44
> 44 0.728 27.33
> 45 0.745 34.22
> 46 0.762 41.55
> 47 0.779 41.55
> 48 0.796 41.55
> 49 0.813 41.53
> 50 0.830 41.53
> 51 0.847 41.52
> 52 0.864 41.52
> 53 0.880 41.53
> 54 0.897 41.53
> 55 0.914 33.07
> 56 0.931 25.12
> 57 0.948 19.25
> 58 0.965 11.30
> 59 0.982 12.48
> 60 0.999 13.85
> 61 1.016 13.62
> 62 1.033 12.62
> 63 1.050 19.39
> 64 1.067 25.48
> 65 1.084 31.06
> 66 1.101 39.49
> 67 1.118 39.48
> 68 1.135 39.46
> 69 1.152 39.45
> 70 1.169 39.43
> 71 1.185 39.42
> 72 1.202 39.42
> 73 1.219 39.41
> 74 1.236 39.41
> 75 1.253 37.39
> 76 1.270 29.03
> 77 1.287 20.61
> 78 1.304 14.07
> 79 1.321  9.12
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list