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

David Winsemius dwinsemius at comcast.net
Thu Mar 5 14:19:04 CET 2009


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.




More information about the R-help mailing list