[R] Odp: testing column data against criteria, point by point

Petr PIKAL petr.pikal at precheza.cz
Thu Mar 5 13:57:00 CET 2009


Hi


r-help-bounces at r-project.org napsal dne 05.03.2009 09:46:17:

> I am fairly new to R and I would like to do the following, but do not 
know 
> where to start. Any help or direction would be appreciated.
> I have a time series of snow depth measurements. I would like to 
determine the
> depth of snowfall for each snowfall event. There is noise in the data so 
I 
> only want to add data values if the subsequent depth is greater than the 

> previous by a certain margin. I am only interested in calculating snow 
> accumulation events.
> Example data:
> 
> Time    depth 
> 1    84.3 
> 2    84.5 
> 3    86 
> 4    86.1 
> 5    85.8 
> 6    86.7 
> 7    87.9 
> 8    89.1 
> 9    90 
> 10    89 
> 11    88 
> 12    88 
> 13    89.1 
> 14    90 
> 15    91.2 
> 16    89.9 
> ...    ... 
> I would like to create a second data frame from the data that looks 
something like this:
> 
> Event    InitialDepth    FinalDepth    Accumulation    InitialTime 
FinalTime 
> 1    84.3    90    5.7    1    9 
> 2    88    91.2    3.2    11    15 
> ... 
> 
> I would like to write a program that progresses through the depth 
column, 
> point by point, to test if (i+1) - i > x. (where I will set x to exlude 
the 
> noise in the data). As long as i+1 is greater than or equal to i, then 
the 
> initial depth stays at the first data point and the final value changes 
to 
> that in i+n. Once the test is false, this indicates the end of the 
event, the 
> accumulation is calculated, all values are saved as event X and a new 
event isstarted. 
> I tried using ifelse(), but I do not know how to move through the data 
and 
> then save the initial and final values and time stamps in another table.

It is probably possible but I would use rle and cumsum

# which of the data are increasing
ind<-rle(c(T,diff(test$depth)>0))

# what is first depth of increasing interval
first<-test$depth[c(1,cumsum(ind$lengths)[!ind$values])]

# what is last depth of increasing interval
last<-test$depth[cumsum(ind$lengths)[ind$values]]

# what is accumulation
last-first
[1]  1.8  4.2  3.2 -3.8
 
the similar applies to evaluation which are first and last times of events 
and/or its duration.

Regards
Petr



> 
> Thank you very much for your time.
> 
> Kara
> 
> 
> 
> 
> 
> ______________________________________________
> 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.




More information about the R-help mailing list