Kara Przeczek
2009-Mar-05 08:46 UTC
[R] testing column data against criteria, point by point
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 is started. 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. Thank you very much for your time. Kara
Petr PIKAL
2009-Mar-05 12:57 UTC
[R] Odp: testing column data against criteria, point by point
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 notknow> where to start. Any help or direction would be appreciated. > I have a time series of snow depth measurements. I would like todetermine the> depth of snowfall for each snowfall event. There is noise in the data soI> 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 lookssomething like this:> > Event InitialDepth FinalDepth Accumulation InitialTimeFinalTime> 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 depthcolumn,> point by point, to test if (i+1) - i > x. (where I will set x to exludethe> noise in the data). As long as i+1 is greater than or equal to i, thenthe> initial depth stays at the first data point and the final value changesto> that in i+n. Once the test is false, this indicates the end of theevent, the> accumulation is calculated, all values are saved as event X and a newevent isstarted.> I tried using ifelse(), but I do not know how to move through the dataand> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
2009-Mar-05 13:19 UTC
[R] testing column data against criteria, point by point
Look at these functions. They should give you the building blocks you need: ?diff # returns a vector of length n-1 #it would be a logical vector if wrapped in the appropriate functions #needs to be padded to line up with dataframes ?rle # returns a list with the lengths and values of the repeats # will work on logical vectors If your data is in a dataframe, snowdepth, with column names time and depth, then this might create a difference column in that dataframe: snowdepth$incr <- c(0, diff(snowdepth$depth)) And this might give you the be a noise marker: snowdepth$noise <- c(FALSE, diff(snowdepth$depth) > x) You should also look at the function dput so you can offer reproducible code and data with further questions. -- David Winsemius On Mar 5, 2009, at 3:46 AM, Kara Przeczek wrote:> 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 is started. > 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. > > 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.