Quantcast

help with rle function on paired data

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

help with rle function on paired data

Steve E.
Dear R Community - I hope you might be able to provide some guidance regarding the use of the rle function. I have a set of time-series data where a measured value is recorded every 30 seconds after the start of an experiment. Many of the measured values repeat and I am interested only in the values when there is a change. If I turn the measured values into a vector, the rle function works perfectly for this but I need also the corresponding time of the value and I am not sure how to use rle on paired data. Below is a brief example to help explain the problem. I thank you in advance for any assistance you might be able to provide. Regards, Steve

Original dataset:

ElpsdTime, DataValue
0, 1
30, 1
60, 1
90, 2
120, 2
150, 3
180, 2
210, 3
240, 3
.
.

Desired dataset:

ElpTime DataValue
0, 1
90, 2
150, 3
180, 2
210, 3
.
.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: help with rle function on paired data

Peter Ehlers
On 2012-06-08 14:33, Steve E. wrote:

> Dear R Community - I hope you might be able to provide some guidance
> regarding the use of the rle function. I have a set of time-series data
> where a measured value is recorded every 30 seconds after the start of an
> experiment. Many of the measured values repeat and I am interested only in
> the values when there is a change. If I turn the measured values into a
> vector, the rle function works perfectly for this but I need also the
> corresponding time of the value and I am not sure how to use rle on paired
> data. Below is a brief example to help explain the problem. I thank you in
> advance for any assistance you might be able to provide. Regards, Steve
>
> Original dataset:
>
> ElpsdTime, DataValue
> 0, 1
> 30, 1
> 60, 1
> 90, 2
> 120, 2
> 150, 3
> 180, 2
> 210, 3
> 240, 3
> .
> .
>
> Desired dataset:
>
> ElpTime DataValue
> 0, 1
> 90, 2
> 150, 3
> 180, 2
> 210, 3
> .
> .

Let's call your dataframe 'dat'. Here's a pretty brute-force way:

   z <- rle(dat[, 2])
   zV <- z$values
   zL <- z$lengths
   idx <- c(0, head(cumsum(zL), -1)) + 1
   newdat <- data.frame(time = dat[idx, 1], value = zV)
   newdat

Peter Ehlers

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: help with rle function on paired data

raishilpa
 hello,
I have a question regarding the function WebCorpus..When I am using this function its showing error: couldnt find the function...I have downloaded the tm package...Can you help me in this regard? Also I want to know how we can data/information about a product or topic from google,facebook etc using R...I have find a command

corpus <- WebCorpus(GoogleFinanceSource("AAPL"))

but this is not working and showing error couldnt find the function GoogleFinanceSource...

please help me ..I will be thankful to you

thanking you in anticipation




Shilpa Rai
MSc.(2011-2013)
Applied Statistics and Informatics
Indian Institute of Technology,Bombay


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: help with rle function on paired data

William Dunlap
In reply to this post by Steve E.
  > ldiff <- function(x)c(TRUE, x[-1]!=x[-length(x)]) # "logical" diff function
  > d[ldiff(d$DataValue), ]
    ElpsdTime DataValue
  1         0         1
  4        90         2
  6       150         3
  7       180         2
  8       210         3

ldiff does the first half of what rle does.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Steve E.
> Sent: Friday, June 08, 2012 2:33 PM
> To: [hidden email]
> Subject: [R] help with rle function on paired data
>
> Dear R Community - I hope you might be able to provide some guidance
> regarding the use of the rle function. I have a set of time-series data
> where a measured value is recorded every 30 seconds after the start of an
> experiment. Many of the measured values repeat and I am interested only in
> the values when there is a change. If I turn the measured values into a
> vector, the rle function works perfectly for this but I need also the
> corresponding time of the value and I am not sure how to use rle on paired
> data. Below is a brief example to help explain the problem. I thank you in
> advance for any assistance you might be able to provide. Regards, Steve
>
> Original dataset:
>
> ElpsdTime, DataValue
> 0, 1
> 30, 1
> 60, 1
> 90, 2
> 120, 2
> 150, 3
> 180, 2
> 210, 3
> 240, 3
> .
> .
>
> Desired dataset:
>
> ElpTime DataValue
> 0, 1
> 90, 2
> 150, 3
> 180, 2
> 210, 3
> .
> .
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/help-with-rle-function-on-
> paired-data-tp4632856.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...